2 Replies Latest reply on Mar 17, 2008 10:04 PM by Keith Naas

    extened CMPC vs SMPC

    Bernix Ning Newbie

      In my application, I want to hold some informations of the login user, so I have a loginInformation component in the session.
      I set the user into the loginInformation component when user login, but the problem is, when I tried to retrieve the children object of user in other backing bean, an error said:
      org.hibernate.LazyInitializationException: could not initialize proxy - no Session


      I am using seam-managed hibernate session


      1. Authenticator.java


      @Name("authenticator")
      public class Authenticator
      {
           @In
           private Session mysession;
           
           @In(creat=true)
           private LoginInformation loginInformation;
           
           public boolean authenticate()
           {
                // find the user from database
                // authentication logic
                loginInformation.set(user);
                return true;
           }
      }



      2. LoginInformation.java


      @Name("loginInformation")
      @Scope(SESSION)
      public class LoginInformation
      {
           private User user;
           // other login information fields
      
           // getters and setters
      }



      3. User.java


      @Entity
      public class User implements Serializable
      {
           private Integer id;
           private String name;
           private String password;
           private Children child;
           
           // getters and setters
      }



      4. MyBackingBean.java


      @Name("uiBackingBean")
      @Scope(CONVERSATION)
      @Restrict("#{identity.loggedIn}")
      public class MyBackingBean
      {
           @In
           private LoginInformation loginInformation;
           
           @Create
           @Begin
           public void init ()
           {
                // Error: could not initialize proxy - no Session
                String childName = loginInformation.getUser().getChild().getName();
                ...
           }
           
           ...
      }




      If I change the Authenticator to use extended CMPC, everything works fine...



      @Name("authenticator")
      public class Authenticator
      {
          @PersistenceContext(type=EXTENDED)
          private EntityManager entityManager;
          ...
      }



      But we may move the database access into the DAO layer in the future, so the extened CMPC is acceptable...


      Any comments will be greatful.

        • 1. Re: extened CMPC vs SMPC
          Bernix Ning Newbie

          so the extened CMPC is acceptable...


          Sorry, missed a word...


          so the extened CMPC is NOT acceptable...


          and the Authenticator after changed should be a EJB component



          @Stateless
          @Name("authenticator")
          public class Authenticator implements IAuthenticator
          {
              @PersistenceContext(type=EXTENDED)
              private EntityManager entityManager;
              ...
          }

          • 2. Re: extened CMPC vs SMPC
            Keith Naas Novice

            So the Authenticator is a Stateless object, which means the entityManager is closed immediately after the authenticate method occurs.  Since the user is associated to a closed session, an LIE occurs. 


            Note in 8.3.3. Seam-managed persistence contexts and atomic conversations that it states



            Persistence contexts scoped to the conversation allows you to program optimistic transactions that span multiple requests to the server without the need to use the merge() operation , without the need to re-load data at the beginning of each request, and without the need to wrestle with the LazyInitializationException or NonUniqueObjectException.

            So, the easiest thing to do is to refresh the user when you need a fresh copy in MyBackingBean.  The user itself is already loaded, but the child is not, so you can just lookup the user using its primary key.