-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: GenericHome for object access
PostPosted: Fri Feb 24, 2006 7:10 am 
Newbie

Joined: Fri Feb 24, 2006 6:31 am
Posts: 1
Location: Atlanta, GA, USA
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;
      }
    }

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.