日期:2014-05-16 浏览次数:20464 次
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* 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.soft863.bbs.dao.hibernate.GenericDaoHibernate">
* <constructor-arg value="com.soft863.bbs.model.Foo"/>
* <property name="sessionFactory" ref="sessionFactory"/>
* </bean>
* </pre>
*
* @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a>
*/
public class GenericDaoHibernate<T, PK extends Serializable> extends
HibernateDaoSupport implements GenericDao<T, PK> {
protected final Log log = LogFactory.getLog(getClass());
private Class<T> persistentClass;
public GenericDaoHibernate() {
}
public GenericDaoHibernate(Class<T> persistentClass) {
this.persistentClass = persistentClass;
}
public void setPersistentClass(Class<T> persistentClass) {
this.persistentClass = persistentClass;
}
@SuppressWarnings("unchecked")
public List<T> getAll() {
return super.getHibernateTemplate().loadAll(this.persistentClass);
}
@SuppressWarnings("unchecked")
private 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;
}
@SuppressWarnings("unchecked")
private boolean exists(PK id) {
T entity = (T) super.getHibernateTemplate().get(this.persistentClass,
id);
if (entity == null) {
return false;
} else {
return true;
}
}
private void remove(PK id) {
this.remove(this.get(id));
}
public T save(T object) {
super.getHibernateTemplate().saveOrUpdate(object);
return object;
}
private void remove(Object object) {
super.getHibernateTemplate().delete(object);
}
@SuppressWarnings("unchecked")
public Object getObject(final Class persistentClass, final String where) {
List list = this.getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session s)
throws HibernateException, SQLException {
StringBuffer sql = new StringBuffer("from "
+ persistentClass.getName() + " p ");
if (StringUtils.isNotBlank(where)) {
if (where.trim().startsWith("where ")) {
sql.append(where);
} else {
sql.append(" where ").append(where);
}
}
Query q = s.createQuery(sql.toString());
return q.list();
}
});
if (list != null) {
// 如果取到唯一一个值,返回
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 0) {
// 如果多余一个值,输出错误,返回null
// System.out.println("[warn]:getObject("+persistentClass.getName()+",\""+where+"\"):根据唯一条件得到多于一个的值,请检查!");
return null;
}
}
return null;
}
@SuppressWarnings("unchecked")
public List getObjs(final String hsql) {
return getObjs(hsql,-1,-1);
}
public List getObjs(final String hsql,f