-->
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: Generic DAO class feedback
PostPosted: Wed Aug 19, 2009 8:27 am 
Newbie

Joined: Sun Aug 16, 2009 5:46 pm
Posts: 4
I came from the world of C++ so my knowledge in Java is not perfect...
Using Hibernate with MyEclipse its create for each POJO also a Dao object.
I thought to create generic DAO object and use it instead of all the specifics objects, sow the samples using templates, but I don't like this option, so I created my own class with few methods so far but looks to work fine.
I'll be happy to get some feedback is this way is good / bad, and if I missed something critical.

The code:
Code:
package test.hibernate;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

public class GenericDao extends BaseHibernateDAO {
   private static final Log log = LogFactory.getLog(KeywordDAO.class);
   
   public String _type;
   public GenericDao(String type){
      _type = type;
   }
   
   public List findAll() {
      log.debug("finding all instances");
      try {
         String queryString = String.format("FROM %s", _type);
         Query queryObject = getSession().createQuery(queryString);
         return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find all failed", re);
         throw re;
      }
   }
   public void delete(Object persistentInstance) {
      log.debug( String.format("deleting %s instance", _type) );
      try {
         getSession().delete(persistentInstance);
         log.debug("delete successful");
      } catch (RuntimeException re) {
         log.error("delete failed", re);
         throw re;
      }
   }
   public void save(Object transientInstance) {
      log.debug( String.format("saving %s instance", _type) );
      try {
         getSession().save(transientInstance);
         log.debug("save successful");
      } catch (RuntimeException re) {
         log.error("save failed", re);
         throw re;
      }
   }
   public void attachDirty(Object instance) {
      log.debug("attaching dirty instance");
      try {
         getSession().saveOrUpdate(instance);
         log.debug("attach successful");
      } catch (RuntimeException re) {
         log.error("attach 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.