1 Reply Latest reply on May 14, 2009 10:37 AM by Tathagat Tathagat

    LazyInitializationException - JPA hibernate tomcat without jboss embedded

    Tathagat Tathagat Newbie

      Hi guys.
      I have a JPA seam application running on tomcat 6.


      Seam version - 2.1.1.GA.


      I think I have SMPC set up properly so as not to get LIE. My code is following. May be you guys see there something I am missing.


      Thanks in advance.



      components.xml





      <?xml version="1.0" encoding="UTF-8"?>
      <components xmlns="http://jboss.com/products/seam/components"
        xmlns:core="http://jboss.com/products/seam/core"
        xmlns:persistence="http://jboss.com/products/seam/persistence"
        xmlns:security="http://jboss.com/products/seam/security"
        xmlns:drools="http://jboss.com/products/seam/drools"
        xmlns:web="http://jboss.com/products/seam/web"
        xmlns:mail="http://jboss.com/products/seam/mail"
        xmlns:transaction="http://jboss.com/products/seam/transaction"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:spring="http://jboss.com/products/seam/spring"
        xmlns:international="http://jboss.com/products/seam/international"
        xmlns:ui="http://jboss.com/products/seam/ui"
        xsi:schemaLocation=
            "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.0.xsd
             http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.0.xsd
             http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.0.xsd
             http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.0.xsd
             http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.0.xsd
             http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.0.xsd
             http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.0.xsd
             http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.0.xsd
             http://jboss.com/products/seam/spring http://jboss.com/products/seam/spring-2.0.xsd
             http://jboss.com/products/seam/international http://jboss.com/products/seam/international-2.0.xsd">
      
           <core:init />
           
           <security:identity authenticate-method="#{authenticator.authenticate}"/>
      
           <transaction:entity-transaction entity-manager="#{em}"/>
           
           <persistence:entity-manager-factory name="rupee2india"/>
           <persistence:managed-persistence-context name="em"
               auto-create="true" 
              entity-manager-factory="#{rupee2india}"
              persistence-unit-jndi-name="java:comp/env/jdbc/TestDB"/>
           
           <event type="org.jboss.seam.security.notLoggedIn">
               <action execute="#{redirect.captureCurrentView}"/>
           </event>
           <event type="org.jboss.seam.security.postAuthenticate">
               <action execute="#{redirect.returnToCapturedView}"/>
           </event>
      
      </components>



      From page 1, user clicks on the following link:


      <h:commandLink action="#{client.home}" value="here"/>



      In my Action class, I start a long running conversation with a dummy method.


      package com.algolog.app.rupee2india.client;
      
      import java.io.Serializable;
      
      import javax.persistence.EntityManager;
      
      import org.apache.log4j.Logger;
      import org.jboss.seam.annotations.Begin;
      import org.jboss.seam.annotations.Create;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      
      import com.algolog.app.rupee2india.security.User;
      
      @Name("client")
      public class ClientAction implements Serializable {
      
           /**
            * 
            */
           private static final long serialVersionUID = 1L;
           
           private static Logger logger = Logger.getLogger(ClientAction.class);
           
           @In EntityManager em;
           @In private User currentUser;
           
           public String home() {
                logger.debug("home: " + currentUser.getEmail());
                
                currentUser = (User) em.createQuery("select u from User u where u.id= :id ")
                .setParameter("id", currentUser.getId())
                .getResultList().get(0);
                return null;
           }
           
           @Begin
           @Create
           public void dummy() {
                
           }
      }
      



      After the home method on class ClientAction, user is directed to page 2, where I want to show addresses associated with this User. I get LIE here. My understanding is that cause I have a long running conversation, SMPC should keep the transaction open and fetch me more data.


      The class User is something like following:


      @Entity
      public class User extends BaseObject {
      .....
      @OneToMany(mappedBy="user")
      public List<Address> getAddresses() {
           return addresses;
      }
      



      What am I missing?


      Thanks again!