org.appfuse.dao.hibernate.GenericDaoHibernate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of appfuse-hibernate Show documentation
Show all versions of appfuse-hibernate Show documentation
AppFuse DAO backend implemented with Hibernate (http://hibernate.org).
package org.appfuse.dao.hibernate;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.dao.GenericDao;
import org.springframework.orm.ObjectRetrievalFailureException;
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.
*
* To register this class in your Spring context file, use the following XML.
*
* <bean id="fooDao" class="org.appfuse.dao.hibernate.GenericDaoHibernate">
* <constructor-arg value="org.appfuse.model.Foo"/>
* <property name="sessionFactory" ref="sessionFactory"/>
* </bean>
*
*
* @author Bryan Noll
*/
public class GenericDaoHibernate extends HibernateDaoSupport implements GenericDao {
protected final Log log = LogFactory.getLog(getClass());
private Class persistentClass;
public GenericDaoHibernate(Class persistentClass) {
this.persistentClass = persistentClass;
}
public List getAll() {
return super.getHibernateTemplate().loadAll(this.persistentClass);
}
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;
}
public void save(T object) {
super.getHibernateTemplate().saveOrUpdate(object);
}
public void remove(PK id) {
super.getHibernateTemplate().delete(this.get(id));
}
}