0

I'm looking for a hibernate plugin that work well with struts2 where can query data using hibernate (JPA) and display it in jsp without violate situation such as hibernate session closed.

I'm using latest struts2, Hibernate (JPA).

Is it better to use Spring DAO or JPA from Hibernate? I prefer JPA.

Is it feasible to use Full HIbernate Plugin with latest struts2 and Hibernate?

Please help.

Thanks.

Roman C
  • 49,761
  • 33
  • 66
  • 176
nicholas
  • 2,581
  • 14
  • 66
  • 104
  • Are you asking something like: https://www.google.com/search?q=integrate+struts2+hibernate? – Pigueiras Oct 03 '12 at 05:40
  • i will go with some other approach like creating a layer say populator which will populate data for UI layer rather than sending connection up to UI layer – Umesh Awasthi Oct 03 '12 at 05:46
  • Spring AOP @Transactional support is good for managing sessions (to prevent errors like Hibernate session closed). It can be used with Hibernate/JPA. You may want to read up on it via the Spring docs http://static.springsource.org/spring/docs/3.0.x/reference/transaction.html#transaction-declarative or search for tutorials using Struts2 + Spring + Hibernate. It's not a plugin solution, though. – Jensen Ching Oct 03 '12 at 08:02
  • Hi Jensen, Do you have any tutorial that use Spring @Transactional and Hibernate(JPA). Thanks. – nicholas Oct 03 '12 at 14:08

1 Answers1

0

In order to accomplish this task you don't need a full hibernate plugin. You are looking to implement the Open Session In View pattern. Basically the idea is to implement a servlet filter that opens a hibernate session, passes control to the rest of the filter chain, and then closes the filter once execution is complete. In this manner your view has an open session to play with, and it gets closed in the end. However, it should be pointed out that some people view this as an anti-pattern, since it does allow your view code to trigger a large number of database related operations via lazy loading and such. This is definitely something to keep in mind.

There are a number of examples online, usually under the abbreviation OSIV, here is one using straight hibernate that I wrote for a former employer that is open sourced. I think the original version of this was written in 2007 or so and recently moved from SVN to GIT:

https://fisheye.5amsolutions.com/browse/5AMCOM/core/src/main/java/com/fiveamsolutions/commons/web/filter/OpenSessionInViewFilter.java?r=78fe9215dcbdea11ed54e7446bf19f779cb13770

The home page to their open source library that includes this is here:

https://www.5amsolutions.com/how-we-do-it/5am-commons

If you want to use a complete JPA solution, spring does include an Open Entity Manager in View filter:

http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/orm/jpa/support/OpenEntityManagerInViewFilter.html

I haven't used that specific class since I haven't used Spring in years, but Spring's stuff usually works very well.

One last link on the topic is a few years old from the hibernate docs: https://community.jboss.org/wiki/OpenSessionInView

Scott Miller
  • 686
  • 6
  • 8
  • Struts 2 <-- (ContextLoaderListener) --> Spring <-- (LocalSessionFactoryBean) --> JPA(Hibernate) If using Spring OpenEntityManagerInViewFilter, then need to use the JPATransactionManager rather than pure JPA. Is this correct? Any tutorial teach how to integrate OpenEntityManagerInViewFilter + JPATransactionManager and inject into Struts2. Thanks – nicholas Oct 04 '12 at 10:50