I've been using the Hibernate Tools to generate *Home.java objects for simple data access. I realized, however, that most of the work that they were doing could be consolidated into a single GenericHome.java through the use of Java 5 generics. Here's how I invoke it:
Code:
GenericHome<Cat> catHome = new GenericHome<Cat>(Cat.class);
Here's the source:
Code:
public class GenericHome<T> {
private static final Log log = LogFactory.getLog(GenericHome.class);
protected final SessionFactory sessionFactory = getSessionFactory();
Class<T> theClass;
/** Need class info at runtime for a couple of the methods
* @param theClass
*/
public GenericHome(Class<T> theClass) {
super();
this.theClass = theClass;
}
protected SessionFactory getSessionFactory() {
return HibernateUtil.getSessionFactory();
}
public void persist(T transientInstance) {
log.debug("persisting instance");
try {
sessionFactory.getCurrentSession().persist(transientInstance);
log.debug("persist successful");
}
catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
public void attachDirty(T instance) {
log.debug("attaching dirty instance");
try {
sessionFactory.getCurrentSession().saveOrUpdate(instance);
log.debug("attach successful");
}
catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(T instance) {
log.debug("attaching clean instance");
try {
sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
}
catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void delete(T persistentInstance) {
log.debug("deleting instance");
try {
sessionFactory.getCurrentSession().delete(persistentInstance);
log.debug("delete successful");
}
catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public T merge(T detachedInstance) {
log.debug("merging instance");
try {
T result = theClass.cast( sessionFactory.getCurrentSession()
.merge(detachedInstance));
log.debug("merge successful");
return result;
}
catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public T findById( final java.lang.Integer id) {
log.debug("getting Stock instance with id: " + id);
try {
T instance = theClass.cast( sessionFactory.openSession()
.get(theClass.getCanonicalName(), id));
if (instance==null) {
log.debug("get successful, no instance found");
}
else {
log.debug("get successful, instance found");
}
return instance;
}
catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(T instance) {
log.debug("finding instance by example");
try {
List results = sessionFactory.getCurrentSession()
.createCriteria(instance.getClass().getCanonicalName())
.add(Example.create(instance))
.list();
log.debug("find by example successful, result size: " + results.size());
return results;
}
catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
/** Gets a list from the given HQL query string
* @return
*/
protected List getQueryList(String queryString, Object... queryParams) {
try {
Query q = sessionFactory.openSession().createQuery(queryString);
for (int i = 0 ; i < queryParams.length; i++ ) {
q.setParameter(i, queryParams[i]);
}
List thicknesses = q.list();
if (thicknesses==null) {
log.debug("get successful, no instance found");
}
else {
log.debug("get successful, instance found");
}
return thicknesses;
}
catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
}