Pages

Tuesday, July 5, 2011

Struts2+Spring+Hibernate and OpenSessionInViewFilter


Problem Faced:

  • I have a struts2 action class (which is declared as a spring bean in application context) to retrieve the users list from the database and to show on the jsp.
  • Used Spring in the middle tier for transaction management (annotation based) and  HibernateDAOSupport form Spring to manage the DAO layer.
  • The action will call a service layer method which is transactional. The service method inturn called the UserDAO  to retrieve the User details. The User class has a relation with Account class.
  • By the time , the service layer method returns, only the User object will be initialized. Because , the Account entity is mapped as “lazy”.
  • In the JSP page, if I try to access Account property of the User, I’ll get LazyInitilizationException (as expected).

Now configured the “OpenSessionInViewFilter” in the web.xml. But still getting the same exception. The transaction is closing the underlying hibernate session once the transaction is completed.
In the logs I could see that the Filter is getting initialized, but no other statements related to it.
After some debugging found that, The OSIV filter is mapped in the web.xml after, struts2 filter. As a result, it is never applied to my urls. Changed the order of filters in web.xml. Now everything is working as expected.

No comments:

Post a Comment