1 Reply Latest reply on Feb 27, 2013 3:16 AM by Alper Tunga

    Seam 2.3 EntityHome and EntityList lazyinitialization problem

    Alper Tunga Newbie

      Hi All,

       

      Our project configuration is:

      JBoss Seam 2.3

      JBoss AS 7.1.2

      JDK 1.7.09

      Our implementation doesn't use EJB concept, we only use backing beans on our implementation.

       

      We would like to use Lazy initialization inside our entity relations. My expectation for Seam was when we get any Home entity

      I don't need to use entity manager for our operations.

       

      The issue comes up when I initialize an object(say User) from the session with the declaration:

      user = (ImtUser) Component.getInstance("authenticatedUser", ScopeType.SESSION)

      and try to reach another entity(say ImtLanguage) which is defined in bidirectional Many-to-One relation,

      I get the lazyinitialization exception.

       

      The problematic backing bean is attached as UserSettingsForm.java

      Entities are attached as ImtUser.java and ImtLanguages.java.

      Log is attached as Log.log.

      In persistence.xml we only have the below statement which is by default set in the file:

      <persistence:managed-persistence-context name="entityManager" auto-create="true"

           entity-manager-factory="@seamEmfRef@"

           persistence-unit-jndi-name="@puJndiName@"/>

       

      We found a solution that requires to use entityManager instead of entityList but this solution seems not logical since it requires us to define an EntityManager and needs manual setting of each object.

      Mainly the code is to inject the EntityManager, get the entity manager instance from the session and set the entity to the returning value of the entityManager find method with the entityClass and Id.

      I give the code to be precise:

       

      @In(create=true)

      private EntityManager entityManager;

      ...

      user = (ImtUser) Component.getInstance("authenticatedUser", ScopeType.SESSION);

      entityManager = (EntityManager) Component.getInstance("entityManager", ScopeType.SESSION);

       

      user = entityManager.find(user.getClass(),user.getUserId());

      entityManager.lock(user,LockModeType.NONE);

      selectedLanguage = imtUser.getImtLanguage().getLanguageName();

       

      This code works and we can reach the content of the ImtLanguage entity but as I say this doesn't look like a best practice at all.

       

      Another solution would be to change all the Lazy relations to Eager but as expected when any get data operation is executed all the database is retrieved.

       

      Can you please help to solve this issue?

       

      Best regards,

       

      Alper Tunga