This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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.
Joined: Nov 24, 2005
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.
Joined: Aug 06, 2006
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 CustomDAO.java 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.
Joined: Nov 24, 2005
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