-->
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.  [ 2 posts ] 
Author Message
 Post subject: A better way to implement DAOs w/o code duplication?
PostPosted: Mon Mar 12, 2012 4:20 pm 
Newbie

Joined: Wed Oct 14, 2009 3:22 pm
Posts: 16
Hi,

I'm using Hibernate 4.0.1.Final and MySql 5.1. Right now, my DAOs are implementations of interfaces I set up. I do this because occassionally during testing I can substitute different mock implementations of those DAOs. However, I'm duplicating code (getting sessions and session factories) and I was wondering how I can design my application more elegantly. For example, in one of my DAOs I have ...

Code:
public class EventFeedsDaoImpl implements EventFeedsDao {

   private static SessionFactory sessionFactory = null;
   
   private static final Logger LOG = Logger.getLogger(EventFeedsDaoImpl.class);

   public EventFeedsDaoImpl() {      
      final Configuration configuration = new Configuration();
       configuration.configure();
       final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
       sessionFactory = configuration.buildSessionFactory(serviceRegistry);
   }   // EventFeedsDaoImpl
   
   @Override
   public Set<EventFeed> getAllEventFeeds() {
      final Session session = this.getSession();
      final Set<EventFeed> eventFeeds = new HashSet<EventFeed>();
      try {
         final Criteria crit = session.createCriteria(EventFeed.class);
         final List results = crit.list();
         if (results != null && results.size() > 0) {
            eventFeeds.addAll( results );
         }   // if
         session.flush();
      } catch (HibernateException e) {
         LOG.error(e.getMessage(), e);
      } finally {
         if (session != null) {
            try {
               session.close();
            } catch (HibernateException e) {
               e.printStackTrace();
            }
         }
      }   // try      
      return eventFeeds;
   }

   private Session getSession() {
      Session session = null;
      try {
         session = sessionFactory.openSession();
      } catch (HibernateException e) {
         e.printStackTrace();
      }
      return session;
   }
   
}


Note that I have the "getSession" method in all my DAO implementations, in addition to the same session factory initialization code. What is a better way to go?

Thanks, - Dave


Top
 Profile  
 
 Post subject: Re: A better way to implement DAOs w/o code duplication?
PostPosted: Tue Mar 13, 2012 9:46 am 
Newbie

Joined: Mon Mar 12, 2012 7:07 am
Posts: 9
I have an intermediate set-up: my DAO objects all have begin(), end() methods that are called by a DB "façade" object that deals with higher level logic.
Now I use those methods for transaction context in the implementation specific to hibernate, but you can use it for opening/closing session.
Right now my sessions are just linked to the lifespan of a DAO object but may be I must change that (because I do not get anything in the database when my code is executed! ;-()
(for your precise question: an abstract class on top of your DAO?)


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

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.