1 Reply Latest reply on Jul 24, 2009 6:51 PM by Arbi Sookazian

    Seam Spring Hibernate Integration

    Carson Black Newbie

      Hello all. I'm a long time Spring/Hibernate user and a first time Seam user. I've managed to get a simple login application up and running where the User Object is retrieved from a Spring Managed Hibernate DAO and then outjected into a Seam session scoped variable.  As you can imagine, I'm running into a lazy-init exception when trying to dig deeper into my User Object upon logging in.


      I'm almost positive my pain is due to my Hibernate session ending prematurely once Seam executes render response.  I would very much prefer that my middle layer stay Spring/Hibernate and Seam only be used in my UI layer to supplement JSF. I have (or had) no plans on using EJB3.  My questions are:


      1.)  Do I need to enable the Spring Open Session in View Filter? If so, does it come before or after the Seam filters?
      2.)  Do I need to configure Seam using a persistence context? As it stands I have not configured anything special in components.xml other than to disable Seam transactions (I'm using Spring/Hibernate transactions).


      I view Seam and Spring as complimentary technologies so I'm very interested in learning best practices for leveraging these two technologies.  Thank you!

        • 1. Re: Seam Spring Hibernate Integration
          Arbi Sookazian Master

          There is a free, online chapter on Seam and Spring integration here: www.manning.com/dallen from SiA book.


          The best way to avoid LIEs is to use an extended container managed-PC (scoped to component life) or SMPC (scoped to conversation life).  Typically in Seam apps, we use SMPC exclusively.  The general goal with an extended-PC is to keep the EntityManager open after a business method call.  With a tx-scoped PC, the EntityManager is closed after a business method call (and thus sets up the LIE trap).


          The goal with SMPC is to keep the EntityManager open until the conversation ends, and you can also use Hibernate manual flush with SMPC, which has advantages as well.  In fact, the SMPC and manual flush combo is the general recommendation in Seam to achieve atomic conversations.


          The other alternative is eager fetching which has disadvantages as well like loading too many entities into the PC.


          As far as how and what to integrate Spring with Seam apps, refer to the above link.  If you have existing Spring beans that you'd like to re-use, then it makes sense to integrate.  Otherwise, you can write your own Seam and/or EJB managed DAO layer.