2 Replies Latest reply on Jan 23, 2012 12:47 AM by David Sing

    AS7, EJB3, JSF, JPA2: LazyInitializationException

    jabu10245 Newbie

      Hello Forum,

       

      I'm new to AS7 etc. and I'm trying to create a little EAR project in the Standalone environment.

      Here's my project configuration (using Maven):

       

      myapp.ear:

      <ear>/META-INF/application.xml:

      [...]
      <module><ejb>myapp-ejb.jar</ejb></module>
      <module><web><web-uri>myapp-jsf.war</web-uri></web></module>
      <library-directory>lib</library-directory>
      

       

      <ear>/lib/commons-lang.jar

       

      <ear>/myapp-ejb.jar

      <ear>/myapp-jsf.war

       

      myapp-ejb.jar:

      <jar>/META-INF/beans.xml => empty beans.xml

      <jar>/META-INF/persistence.xml:

      [...]
      <persistence-unit name="primary">
          <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
          <properties>
              <property name="hibernate.hbm2ddl.auto" value="create-drop" />
          </properties>
      </persistence-unit>
      

       

      and an EJB (for testing purpose as singleton):

      @LocalBean
      @Named("fooManager")
      @Singleton @Startup
      public class FooManagerBean implements FooManager {
          private @PersistenceContext EntityManager entityManager;
      
          public Foo getFoo(String name) { /* JPA lookup using entityManager */ }
      }
      

      The class Foo is just a mapped entity having Foos as children:

       

      @Entity @Table(name = "foo")
      public class Foo implements Serializable {
          @Column(name = "name", nullable = false, unique = true, length = 64)
          private String name;
      
          @ManyToOne
          private Foo parent;  // nullable
      
          @OneToMany(mappedBy = "parent")
          private List<Foo> children;
      
          // Getters and Setters, hashCode, equals, toString
      }
      

       

       

      myapp-jsf.war:

      <war>/WEB-INF/beans.xml => empty beans.xml

      <war>/WEB-INF/faces-config.xml => empty JSF 2.0 config xml

      <war>foo.xhtml => displays all root foos with all their children => LazyInitializationException

       

      @Named @RequestScoped
      public class FooController implements Serializable {
      [...]
      }
      

       

      The FooController uses the

      @EJB FooManager
      to load all root Foo objects, the foo.xhtml should display them with

      all their children. The root Foo objects are being loaded correctly, but when I try to access the

      children a LazyInitializationException is being thrown, because I access them outside the transaction

      scope of the Entity Manager from the FooManager.

       

      My question is: Is there something like Spring's OpenEntityManagerInViewInterceptor?

       

      I googled it and found some descriptions using Seam, but I have no experience with that. Is Seam

      party of JBoss AS7? Or do I have to include it in my WAR as a dependency? What version should I

      use? Or is there any simpler way solving my problem?

       

       

      Thanks to anyone who can point me to a solution ;-)