• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

spring 2 with hibernate 3

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

http://static.springframework.org/spring/docs/2.0.x/reference/orm.html#orm-hibernate-straight

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>

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

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

<tx:advice id="txAdvice" transaction-manager="hbManager">
<tx:attributes>
<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"/>
</tx:attributes>
</tx:advice>

*************************

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

session.close();


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.
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
sachin yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic