Hibernate 通用类

GenericDaoHibernate.java

package com.shadow.oa.dao.hibernate;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.shadow.oa.dao.GenericDao;

/**
* This class serves as the Base class for all other DAOs - namely to hold
* common CRUD methods that they might all use. You should only need to extend
* this class when your require custom CRUD logic.
*
* <p>
* To register this class in your Spring context file, use the following XML.
*
* <pre>
* <bean id="fooDao" class="com.shadow.app.dao.hibernate.GenericDaoHibernate">
* <constructor-arg value="com.shadow.app.model.Foo"/>
* <property name="sessionFactory" ref="sessionFactory"/>
* </bean>
* </pre>
*
* @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a>
* @param <T>
* a type variable
* @param <PK>
* the primary key for that type
*/
public class GenericDaoHibernate<T, PK extends Serializable> extends
HibernateDaoSupport implements GenericDao<T, PK> {
/**
* Log variable for all child classes. Uses LogFactory.getLog(getClass())
* from Commons Logging
*/
protected final Log log = LogFactory.getLog(getClass());
private Class<T> persistentClass;

/**
* Constructor that takes in a class to see which type of entity to persist
*
* @param persistentClass
* the class type you'd like to persist
*/
public GenericDaoHibernate(final Class<T> persistentClass) {
this.persistentClass = persistentClass;
}

/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public List<T> getAll() {
return super.getHibernateTemplate().loadAll(this.persistentClass);
}

/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public List<T> getAllDistinct() {
Collection result = new LinkedHashSet(getAll());
return new ArrayList(result);
}

/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public T get(PK id) {
T entity = (T) super.getHibernateTemplate().get(this.persistentClass,
id);

if (entity == null) {
log.warn("Uh oh, '" + this.persistentClass + "' object with id '"
+ id + "' not found...");
throw new ObjectRetrievalFailureException(this.persistentClass, id);
}

return entity;
}

/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public boolean exists(PK id) {
T entity = (T) super.getHibernateTemplate().get(this.persistentClass,
id);
return entity != null;
}

/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public T save(T object) {
return (T) super.getHibernateTemplate().merge(object);
}

/**
* {@inheritDoc}
*/
public void remove(PK id) {
super.getHibernateTemplate().delete(this.get(id));
}

/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public List<T> findByNamedQuery(String queryName,
Map<String, Object> queryParams) {
String[] params = new String[queryParams.size()];
Object[] values = new Object[queryParams.size()];
int index = 0;
Iterator<String> i = queryParams.keySet().iterator();
while (i.hasNext()) {
String key = i.next();
params[index] = key;
values[index++] = queryParams.get(key);
}
return getHibernateTemplate().findByNamedQueryAndNamedParam(queryName,
params, values);
}

@SuppressWarnings("unchecked")
public List<T> find(final String hsql, final int firstRow, final int maxRow)
throws Exception {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException,
SQLException {
Query query = s.createQuery(hsql);
query.setFirstResult(firstRow);
query.setMaxResults(maxRow);
List list = query.list();
return list;
}
});
}

public Integer getNum(final Object obj, final String field, final Integer id) {
return (Integer) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session s)
throws HibernateException, SQLException {
Criteria criteria = s.createCriteria(obj.getClass());
criteria.setProjection(Projections.rowCount());
criteria.add(Restrictions.eq(field, id));
return criteria.uniqueResult();
}
});
}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值