All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.appfuse.dao.hibernate.GenericDaoHibernate Maven / Gradle / Ivy

There is a newer version: 3.5.0
Show newest version
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)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy