hibernate 泛型DAO
package net.payj.base;
import static org.hibernate.EntityMode.POJO;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import net.payj.common.bean.Page;
import net.payj.util.ReflectionUtil;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.CacheMode;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.impl.CriteriaImpl;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.transform.ResultTransformer;
import org.springframework.stereotype.Repository;
@Repository
public class BaseDaoImpl<T, PK extends Serializable> implements BaseDao<T, PK> {
private Class<T> entityClass;
protected SessionFactory sessionFactory;
public BaseDaoImpl() {
this.entityClass = ReflectionUtil.getSuperClassGenricType(getClass());
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
public T get(PK id) {
return (T) getSession().get(entityClass, id);
}
@SuppressWarnings("unchecked")
public T load(PK id) {
return (T) getSession().load(entityClass, id);
}
@SuppressWarnings("unchecked")
public List<T> findByHql(String hql, Object... values) {
return createQuery(hql, values).list();
}
@SuppressWarnings("unchecked")
public List<T> findAll() {
String hql = "from " + entityClass.getName();
return createQuery(hql).list();
}
@SuppressWarnings("unchecked")
public List<T> findAll(boolean isCacheable) {
String hql = "from " + entityClass.getName();
return createQuery(hql).setCacheable(isCacheable).list();
}
@SuppressWarnings("unchecked")
public List<T> findAllByCache() {
String cacheRegion = "eternalCacheRegion";// 设置cacheRegion
String hql = "from " + entityClass.getName();
return createQuery(hql).setCacheable(true).setCacheRegion(cacheRegion).list();
}
@SuppressWarnings("unchecked")
public List findAllProperty(String propertyName) {
String hql = "select model." + propertyName + " from " + entityClass.getName() + " model";
Query query = getSession().createQuery(hql);
return query.list();
}
public List<T> findByIds(PK[] ids) {
List<T> list = new ArrayList<T>();
for (PK id : ids) {
T entity = load(id);
if (entity != null) {
list.add(entity);
}
}
return list;
}
public List<T> findByProperty(String propertyName, Object value) {
String hql = "from " + entityClass.getName() + " as model where model." + propertyName + "=?";
return findByHql(hql, value);
}
@SuppressWarnings("unchecked")
public T findUniqueByProperty(String propertyName, Object value) {
String hql = "from " + entityClass.getName() + " as model where model." + propertyName + "=?";
return (T) createQuery(hql, value).uniqueResult();
}
public int findTotalCount() {
String hql = "select count(*) from " + entityClass.getName();
return Integer.parseInt(createQuery(hql).uniqueResult().toString());
}
public boolean isExist(String propertyName, Object value) {
List<T> list = findByProperty(propertyName, value);
- 推荐内容
-
- jquery.validate 自定义验证方法
$(document).ready( function() { // 字符最小长度验证(一个中文字符长...
- jQuery.validate 中文API
jQuery.validate 中文API ( HappyCZX ) 名称 返回类型 描述 validate(op...
- jQuery.validate 用法
名称返回类型描述 validate(options)返回:Validator验证所选的FORM val...
- jquery.validate 自定义验证方法