Sure.
Code:
import java.util.Collections;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Projections;
public class DataAccessObject<E extends Comparable<? super E>>
{
protected static final Log log = LogFactory.getLog(DataAccessObject.class);
protected Class<? extends E> entityClass;
public DataAccessObject(Class<? extends E> _entityClass)
{
entityClass = _entityClass;
HibernateUtil.beginTransaction();
}
public E getById(Long id)
{
Session session = HibernateUtil.getSession();
@SuppressWarnings("unchecked")
E entity = (E)session.load(entityClass, id);
return entity;
}
public E getByExample(E example)
{
Session session = HibernateUtil.getSession();
Criteria crit = session.createCriteria(entityClass)
.add(Example.create(example));
@SuppressWarnings("unchecked")
E entity = (E)crit.uniqueResult();
return entity;
}
public List<E> getAll()
{
return getAll(true, true);
}
public List<E> getAll(boolean cacheable, boolean sort)
{
Session session = HibernateUtil.getSession();
Criteria crit = session.createCriteria(entityClass).setCacheable(cacheable);
@SuppressWarnings("unchecked")
List<E> entities = crit.list();
if (sort)
Collections.sort(entities);
return Collections.unmodifiableList(entities);
}
public List<E> getAllByExample(E example)
{
return getAllByExample(example, false, true);
}
public List<E> getAllByExample(E example, boolean cacheable, boolean sort)
{
Session session = HibernateUtil.getSession();
Criteria crit = session.createCriteria(entityClass)
.add(Example.create(example))
.setCacheable(cacheable);
@SuppressWarnings("unchecked")
List<E> entities = crit.list();
if (sort)
Collections.sort(entities);
return Collections.unmodifiableList(entities);
}
public int getCount()
{
Session session = HibernateUtil.getSession();
Criteria crit = session.createCriteria(entityClass)
.setProjection(Projections.rowCount());
return (Integer)crit.uniqueResult();
}
public int getCount(E entity)
{
Session session = HibernateUtil.getSession();
Criteria crit = session.createCriteria(entityClass)
.setProjection(Projections.rowCount())
.add(Example.create(entity));
return (Integer)crit.uniqueResult();
}
public boolean isExists(E entity)
{
return getCount(entity) > 0;
}
public void save(E entity)
{
if (log.isInfoEnabled())
log.info("Persisting " + entity);
Session session = HibernateUtil.getSession();
session.saveOrUpdate(entity);
}
public void delete(E entity)
{
if (log.isInfoEnabled())
log.info("Deleting " + entity);
Session session = HibernateUtil.getSession();
session.delete(entity);
}
public static void attach(Object entity)
{
if (entity != null)
{
Session session = HibernateUtil.getSession();
if (!session.contains(entity))
session.lock(entity, LockMode.NONE);
}
}
public static void detach(Object entity)
{
if (entity != null)
{
Session session = HibernateUtil.getSession();
session.flush();
session.evict(entity);
}
}
}