Java tutorial
/* * Copyright (c) 2015-2020, Chen Rui * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.rapid.develop.core.jersey.features.hibernate; import com.rapid.develop.core.jersey.features.hibernate.session.HibernateSessionFactory; import com.rapid.develop.core.utils.ReflectionUtils; import org.hibernate.Query; import org.hibernate.Session; import org.jvnet.hk2.annotations.Contract; import javax.inject.Inject; import java.util.List; @Contract public class GenericDao<T extends AbstractEntity> { private Class<T> entityClass = ReflectionUtils.getSuperClassGenricType(this.getClass()); @Inject HibernateSessionFactory hibernateSessionFactory; private Session getCurrentSession() { Session session = hibernateSessionFactory.getSessionFactory().getCurrentSession(); session.enableFilter("deleteFlagFilter"); return session; } public void save(T entity) { this.getCurrentSession().save(entity); } public void persist(T entity) { this.getCurrentSession().persist(entity); } public void update(T entity) { this.getCurrentSession().update(entity); } public void merge(T entity) { this.getCurrentSession().merge(entity); } public void remove(T entity) { entity.setDeleted(true); this.getCurrentSession().update(entity); } public T find(String id) { return this.getCurrentSession().get(entityClass, id); } public Boolean exsits(String id) { String sql = "select count(id) from " + entityClass.getSimpleName() + " where id=:id"; Query query = this.createQuery(sql); query.setParameter("id", id); Long count = (Long) query.uniqueResult(); return count > 0; } public List<T> list() { String sql = "from " + entityClass.getSimpleName() + " where deleted = false order by createDate"; Query query = this.createQuery(sql); query.setCacheable(true); return query.list(); } public void flush() { this.getCurrentSession().flush(); } public void clear() { this.getCurrentSession().clear(); } public Query createQuery(String sql) { Query query = this.getCurrentSession().createQuery(sql); query.setCacheable(false); return query; } public Query createQuery(String sql, Boolean cacheable) { Query query = this.getCurrentSession().createQuery(sql); query.setCacheable(cacheable); return query; } public Query createQuery(String sql, Object... values) { Query query = this.getCurrentSession().createQuery(sql); for (int i = 0; i < values.length; i++) { query.setParameter(i, values[i]); } query.setCacheable(false); return query; } public Query createQuery(String sql, Boolean cacheable, Object... values) { Query query = this.getCurrentSession().createQuery(sql); for (int i = 0; i < values.length; i++) { query.setParameter(i, values[i]); } query.setCacheable(cacheable); return query; } }