I'm using the following DAO implementation for hibernate, which
I'd like to share on this forum to get other people opinions.
It is using java 5.0 generics and is based on the code found at
http://www.ericburke.com/blog/2004/11/h ... ava-5.html
with the difference that id class is also parameterized.
Basically when creating your own DAO you usually only need to write
finder methods
Here is an example of using this generic DAO class
public class CategoryDAO extends GenericDAO<Category, Long> {
public CategoryDAO() {
super(Category.class, Long.class);
}
public List<Category> getRootCategories() {
return get(Expression.isNull("parentCategory"));
}
}
Please, let know what do you this of this approach.
Thanks a lot
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
public abstract class GenericDAO<T, K extends Serializable> {
private Class persistentClass;
private Class idClass;
private static Log log = LogFactory.getLog(GenericDAO.class);
protected Session currentSession() {
return HibernateSessionUtil.currentSession();
}
/**
* Constructor
*
* @param persistentClass
* @param idClass
*/
public GenericDAO(Class persistentClass, Class idClass) {
this.persistentClass = persistentClass;
this.idClass = idClass;
currentSession().beginTransaction();
}
/**
* @return Returns the idClass.
*/
public Class getIdClass() {
return idClass;
}
/**
* @return Returns the persistentClass.
*/
public Class getPersistentClass() {
return persistentClass;
}
/**
* Persist entity in database
*
* @param entity
*/
public void persist(T entity) {
Session session = null;
try {
session = currentSession();
session.persist(entity);
} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}
}
/**
* Save entity in database
*
* @param entity
*/
public void save(T entity) {
Session session = null;
try {
session = currentSession();
session.save(entity);
} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}
}
/**
* Save or update entity in database
*
* @param entity
*/
public void saveOrUpdate(T entity) {
Session session = null;
try {
session = currentSession();
session.saveOrUpdate(entity);
} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}
}
/**
* Delete entity in database
*
* @param entity
*/
public void delete(T entity) {
Session session = null;
try {
session = currentSession();
session.delete(entity);
} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}
}
/**
* Delete all entities
*/
public void deleteAll() {
Session session = null;
try {
session = currentSession();
session.delete("from " + getPersistentClass().getName() + " x");
} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}
}
/**
* Find entity by ID
*
* @param id
* @param lock
* @return entity
*/
@SuppressWarnings("unchecked")
public T getById(K id, boolean lock) {
T entity = null;
try {
if (lock) {
entity = (T) currentSession().get(getPersistentClass(), id, LockMode.UPGRADE);
}
else {
entity = (T) currentSession().get(getPersistentClass(), id);
}
} catch (HibernateException ex) {
log.error(ex.getMessages());
throw new DaoException(ex);
}
return entity;
}
/**
* Get all
*
* @return list of entities
*/
@SuppressWarnings("unchecked")
public List<T> getAll() {
List<T> list = null;
Session session = null;
Query query = null;
try {
session = currentSession();
query = session.createQuery("from " + getPersistentClass().getName() + " x");
list = query.list();
} catch (HibernateException ex) {
throw new DaoException(ex);
}
return list;
}
/**
* This is the cool finder method
*
* @param criterion
* @return list of entities
*/
@SuppressWarnings("unchecked")
public List<T> get(Criterion... criterion) {
List<T> list = null;
Criteria criteria = currentSession().createCriteria(getPersistentClass());
try {
for (Criterion c : criterion) {
criteria.add(c);
}
list = new ArrayList<T>(criteria.list());
} catch (HibernateException ex) {
throw new DaoException(ex);
}
return list;
}
}