File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Application Frameworks and the fly likes spring 2 with hibernate 3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "spring 2 with hibernate 3 " Watch "spring 2 with hibernate 3 " New topic

spring 2 with hibernate 3

Srinivas Ramgopal
Ranch Hand

Joined: Aug 06, 2006
Posts: 63
I am new to Spring.

The following link talks about implementing Spring declarative transaction management around an application's Hibernate based DAOs which are based on plain Hibernate3 API; Thus avoiding usage of Spring API in an application's DAO java classes.

Based on the above link, I setup my config file and a DAO java class whose CRUD methods in turn call the appropriate methods on the Session object. But none of the crud operations are being Commited into the database.

Below is the code snippet:

A part of my xml config file:

<bean id="hbManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>

<bean id="myCustomDAO" class="x.y.LocalDAO">
<property name="sessionFactory" ref="sessionFactory"/>

<aop ointcut id="xMethods" expression="execution(* x.y.LocalDAO.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="xMethods"/>

<tx:advice id="txAdvice" transaction-manager="hbManager">
<tx:method name="create" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
<tx:method name="get" propagation="REQUIRED" read-only="true"/>


In my localDao class, I get the Session from the Sessionfactory and close after each database all as follows:

Session session = sessionFactory.openSession();

//some database call


Question: Do I need to do as above in the DAO class?

If not, is there a way to reuse the Session created by HibernatePersistenceManager (please refer to the config xml code snippet above) in my DAO class in order to call the CRUD methods on that Session object???

Also are any changes needed in the config xml?

Any code snippets are highly helpful.

Thanks in advance for your valuable time and interest.
sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156
Two things to mention:

1). Can you please your whole code.
2). In spring while using ORM tool such as hibernate, we can always delegate the responsibility of Session creation, CRUDE operations and manageing transactions to Spring. Just create a transaction manager, attach your DAO to a TransactionProxyBean and inject your model class into that using AOP. Something like this:

I hope this will help.
sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156
don't do this in your model class

Let Spring do that. And don't forget to explicitly create and pass your command object to model. I face the same problem just because i didn't put the below line.
Srinivas Ramgopal
Ranch Hand

Joined: Aug 06, 2006
Posts: 63
I was able to do CRUD operations successfully after calling the appropriate methods on sessionFactory.currentSession().

The code is attached below:

Config xml code snippet:

In code:

But I now see a weird problem- In a workflow, A.x() calls CustomDAO.callCRUD() which in turn calls the other methods - insert()/get() methods in CustomDAO;
Spring seem to start a transaction only on callCRUD(). In other words, if I if comment out the transaction attribute setting entry related to callCRUD() in the above xml and modify the above java code as follows:

I get the following exception during the runtime:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

Now my question:
Why does Spring start a transaction only on a method that is called by a different class??? I expected it to run the other methods (insert/get) to run in a transaction since they were set in the config file.

I really do not understand as what is missing here.

Any help is highly appreciated.

Thanks a ton in advance.
sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156
see the problem here is that you are trying to do a hibernate transaction while there is no object is attached to the current session thread.
as hibernate picks up it's persistance object from the session itself so you can understand why no database action is happening. And the second part of error says that you are not allowed to create one session here. what you are doing is you are making a hibernate session by creating a new session instance may be spring's current session won't let you do so (i am not sure). Now the solution is as i have state in my first reply, delegate the responsibility of session managment to spring and access the spring session to create the hibernate transaction. Attach your DAO with transaction manager and inject the session into DAO itself.

I might be wrong in my above explaination as i am also new to spring. i suggest you to put some more time on my above code i posted earlier and do it that way, if still problem let me know i will be there
I agree. Here's the link:
subject: spring 2 with hibernate 3