-->
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.  [ 4 posts ] 
Author Message
 Post subject: Should we always use Spring with Hibernate?
PostPosted: Thu Mar 17, 2005 12:19 pm 
Regular
Regular

Joined: Tue Nov 30, 2004 4:23 pm
Posts: 62
I have gotten alot of feedback about architecture questions from both the Hibernate and Spring forums...I really appreciate it!!!

I just have another question. I have been researching Hibernate and am doing a Brown Bag on it for my team to show that it is the best persistence solution out there for our purposes. But I worked on Hibernate on another project and I know we had to write a few classes to control the creation of SessionFactories, classes for flushing and closing Sessions, and other Hibernate helper type classes. Then I began reading about Spring and quite a few people rave about the benefits it adds for integrating Hibernate into a system. Is it recommended when creating an architecture from scratch to utilize Hibernate with Spring....to use Spring for controlling things like the creation of SessionFactory and other Hibernate related features?

Thanks,

jay


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 12:30 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
I use spring, but not with hibernate - hibernate have good API and spring is only envelope for hibernate API

You can use spring, of course

regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 2:40 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Spring wrappers were a good idea for Hibernate 2.x if you didn't want to write a few things yourself. With Hibernate3 you get:

- unchecked exceptions, no need to wrap

- typed exception hierarchy, not much practical use but nice in some situations

- auto flushing and auto closing of the Hibernate Session in JTA environments

- a lot more...

This is the only code you need to startup Hibernate, put it in any class you like (the static block is loaded once):

Code:
   public static SessionFactory sessionFactory;
   static {
      try {
         sessionFactory = new Configuration().configure().buildSessionFactory();
      } catch (Throwable e) {
         throw new ExceptionInInitializerError(e);
      }
   }


Now open Sessions from this factory whenever you need, if you run with EJB Session beans they will be flushed and closed automatically when your container transaction ends.

Another common thing people do is the "Open Session in View" with the "ThreadLocal Session" pattern. Download CaveatEmptor from http://caveatemptor.hibernate.org/ and have a look at the HibernateUtil class. It's about 50 lines of trivial code (open/close) around two ThreadLocal variables for the Session and Transaction that can be easily reduced to 25 if you don't need it verbose. Use some interceptor (e.g. from your web framework or from your EJB container) to handle the Session and Transaction in whatever scope you like.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 3:12 pm 
Regular
Regular

Joined: Mon Sep 29, 2003 9:39 am
Posts: 67
If you use Spring and wire up your dao dependencies thru interfaces in the bean factory, it becomes trivial to switch dao implementations or to mix them.

I'm not sure if it's still there, but one of the best practices in the Hibernate reference was to abstract your hibernate code away from your domain objects. Spring makes this very simple.

By using the HibernateDaoSupport class as a subclass for your HibernateDao's, you can implement them very quickly. If you extract an interface, it's trivial to implement a JDBC version or JDO version of your dao.

None of your classes that need to use the dao will be aware that you are using a JdbcDao or a HibernateDao.

This becomes a selling point for hibernate: if you decide not to use it, reimplement your dao's with another strategy. If you have XP'ers on your brown-bag session, you can relate it in terms of reducing the cost of change by eliminating dependencies.

MyServiceObject -> MyDao
myDao <= MyHibernateDao
myDao <= MyJdoDao
myDao <= MyJdbcDao

Just implement your dao, update the spring config file, and restart your app.

An added benefit is that it makes testing easy using an alt spring config file:
MyServiceObject->MyDao
myDao <= MyMockDao

So, while hibernate 3 makes using hibernate easier like using hibernate through Spring, Spring still has the added benefit of making it easier to swap out hibernate for something else if necessary (and of course, it won't be!).

I think that saying 'always' might be a little drastic.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.