When you use Hibernate and DAO pattern it is a good idea to use a Generic Base Dao. The fallowing code snippet contains GenericDAO that is a base class for all my DAO classes. This GenericDAO uses HibernateDaoSupport from Spring for its implementation if you want you can use JpaDaoSupport or JdbcDaoSupport in your projects.
My Generic DAO interface looks like this :
package org.joke.myproject.dao.base;
import java.io.Serializable;
import java.util.List;
/**
* @author Naiden Gochev
* @param <E>
* @param <PK>
*/
public interface GenericDao<E,PK extends Serializable> {
PK save(E newInstance);
void update(E transientObject);
void saveOrUpdate(E transientObject);
void delete(E persistentObject);
E findById(PK id);
List<E> findAll();
List<E> findAllByProperty(String propertyName,Object value);
}
All method names are very common so I don’t think they need some explanation.
The implementation of this GenericDAO :
package org.joke.myproject.dao.base;
import java.io.Serializable;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* @author JOKe
* @param <E>
* @param <PK>
*/
public abstract class GenericDaoImpl<E, PK extends Serializable> extends
HibernateDaoSupport implements GenericDao<E, PK> {
@SuppressWarnings("unchecked")
public PK save(E newInstance) {
return (PK) getHibernateTemplate().save(newInstance);
}
@SuppressWarnings("unchecked")
public E findById(PK id) {
return (E) getHibernateTemplate().get(getEntityClass(), id);
}
@SuppressWarnings("unchecked")
public List<E> findAll() {
return getHibernateTemplate().findByCriteria(createDetachedCriteria());
}
@SuppressWarnings("unchecked")
public List<E> findAllByProperty(String propertyName, Object value) {
DetachedCriteria criteria = createDetachedCriteria();
criteria.add(Restrictions.eq(propertyName, value));
return getHibernateTemplate().findByCriteria(criteria);
}
public List<E> findByExample(E object) {
List<E> resultList = getHibernateTemplate().findByExample(object, 0, 1);
return resultList;
}
public List<E> findByExample(E object, int firstResult, int maxResults) {
List<E> resultList = getHibernateTemplate().findByExample(object,
firstResult, maxResults);
return resultList;
}
public void update(E transientObject) {
getHibernateTemplate().update(transientObject);
}
public void saveOrUpdate(E transientObject) {
getHibernateTemplate().saveOrUpdate(transientObject);
}
public void delete(E persistentObject) {
getHibernateTemplate().delete(persistentObject);
}
protected abstract Class<E> getEntityClass();
protected DetachedCriteria createDetachedCriteria() {
return DetachedCriteria.forClass(getEntityClass());
}
}
The GenericDaoImpl is abstract because the actual DAOs will extend it and will override only one method : protected abstract Class<E> getEntityClass(); This method is used for creating of the DetachedCriteria object in the createDetachedCriteria() method.
Every DAO class that implements GenericDaoImpl will look like this :
package org.joke.myproject.dao;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.joke.myproject.dao.base.GenericDaoImpl;
import org.joke.myproject.entity.Message;
public class MessagesDAO extends GenericDaoImpl<Message, Integer> {
@Override
protected Class<Message> getEntityClass() {
return Message.class;
}
public List<Message> findNotDeletedTextMessages() {
DetachedCriteria criteria = createDetachedCriteria();
criteria.add(Restrictions.eq("deleted", false));
criteria.add(Restrictions.or(Restrictions.eq("custom", false), Restrictions.isNull("custom")));
List<Message> textMessages = getHibernateTemplate().findByCriteria(criteria);
return textMessages;
}
}
The method here called findNotDeletedTextMessages() is optional it is just another DAO method added in the implementation. If you use Spring you can just add the annotation @Component as class annotation and you have DAO ready for use.
That’s all have fun.
Edit : Check out my next article called JSF, Spring, Hibernate – lets make development easy. It will use this GenericDAO to make architecture of an application.
When you use Hibernate and DAO pattern it is a good idea to use a Generic Base Dao. The fallowing code snippet contains GenericDAO that is a base class for all my DAO classes. This GenericDAO uses HibernateDaoSupport from Spring for its implementation if you want you can use JpaDaoSupport or JdbcDaoSupport in your projects.
My Generic DAO interface looks like this :
package org.joke.myproject.dao.base;
import java.io.Serializable;
import java.util.List;
/**
* @author Naiden Gochev
* @param <E>
* @param <PK>
*/
public interface GenericDao<E,PK extends Serializable> {
PK save(E newInstance);
void update(E transientObject);
void saveOrUpdate(E transientObject);
void delete(E persistentObject);
E findById(PK id);
List<E> findAll();
List<E> findAllByProperty(String propertyName,Object value);
}
All method names are very common so I don’t think they need some explanation.
The implementation of this GenericDAO :
package org.joke.myproject.dao.base;
import java.io.Serializable;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* @author JOKe
* @param <E>
* @param <PK>
*/
public abstract class GenericDaoImpl<E, PK extends Serializable> extends
HibernateDaoSupport implements GenericDao<E, PK> {
@SuppressWarnings("unchecked")
public PK save(E newInstance) {
return (PK) getHibernateTemplate().save(newInstance);
}
@SuppressWarnings("unchecked")
public E findById(PK id) {
return (E) getHibernateTemplate().get(getEntityClass(), id);
}
@SuppressWarnings("unchecked")
public List<E> findAll() {
return getHibernateTemplate().findByCriteria(createDetachedCriteria());
}
@SuppressWarnings("unchecked")
public List<E> findAllByProperty(String propertyName, Object value) {
DetachedCriteria criteria = createDetachedCriteria();
criteria.add(Restrictions.eq(propertyName, value));
return getHibernateTemplate().findByCriteria(criteria);
}
public List<E> findByExample(E object) {
List<E> resultList = getHibernateTemplate().findByExample(object, 0, 1);
return resultList;
}
public List<E> findByExample(E object, int firstResult, int maxResults) {
List<E> resultList = getHibernateTemplate().findByExample(object,
firstResult, maxResults);
return resultList;
}
public void update(E transientObject) {
getHibernateTemplate().update(transientObject);
}
public void saveOrUpdate(E transientObject) {
getHibernateTemplate().saveOrUpdate(transientObject);
}
public void delete(E persistentObject) {
getHibernateTemplate().delete(persistentObject);
}
protected abstract Class<E> getEntityClass();
protected DetachedCriteria createDetachedCriteria() {
return DetachedCriteria.forClass(getEntityClass());
}
}
The GenericDaoImpl is abstract because the actual DAOs will extend it and will override only one method : protected abstract Class<E> getEntityClass(); This method is used for creating of the DetachedCriteria object in the createDetachedCriteria() method.
Every DAO class that implements GenericDaoImpl will look like this :
package org.joke.myproject.dao;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.joke.myproject.dao.base.GenericDaoImpl;
import org.joke.myproject.entity.Message;
public class MessagesDAO extends GenericDaoImpl<Message, Integer> {
@Override
protected Class<Message> getEntityClass() {
return Message.class;
}
public List<Message> findNotDeletedTextMessages() {
DetachedCriteria criteria = createDetachedCriteria();
criteria.add(Restrictions.eq("deleted", false));
criteria.add(Restrictions.or(Restrictions.eq("custom", false), Restrictions.isNull("custom")));
List<Message> textMessages = getHibernateTemplate().findByCriteria(criteria);
return textMessages;
}
}
The method here called findNotDeletedTextMessages() is optional it is just another DAO method added in the implementation. If you use Spring you can just add the annotation @Component as class annotation and you have DAO ready for use.
That’s all have fun.
Edit : Check out my next article called JSF, Spring, Hibernate – lets make development easy. It will use this GenericDAO to make architecture of an application.
Recent Comments