11 Replies Latest reply on Oct 4, 2005 2:46 AM by Louis Coetzee

    Lazyload configuration?

    Louis Coetzee Apprentice

      Hi all,

      I currently get the following exception

      ERROR [LazyInitializationException] failed to lazily initialize a collection of role: csir.league.seam.Team.members, no session or session was closed
      when trying to access a OneToMany collection.

      The OneToMany is defined between Team having a list containing the members (Player)

      
      @OneToMany (cascade = {CascadeType.ALL})
       @JoinTable (table=@Table(name="team_player"), joinColumns=@JoinColumn(name="team_id"), inverseJoinColumns=@JoinColumn(name="player_id") )
       public List<Player> getMembers() {
       return members;
       }
      


      I have configured Seam as follows:

      changed faces-config.xml to use the SeamExtendedManagedPersistencePhaseListener

      <lifecycle> <phase-listener>org.jboss.seam.jsf.SeamExtendedManagedPersistencePhaseListener</phase-listener>
       </lifecycle>
      


      changed web.xml (TrySeam1 is the name defined in persistence.xml)
      
       <context-param>
      <param-name>org.jboss.seam.init.managedPersistenceContexts</param-name>
      <param-value>TrySeam1</param-value>
      </context-param>
      <context-param>
      <param-name>TrySeam1.persistenceUnitName</param-name>
      <param-value>java:/EntityManagerFactories/trySeamData</param-value>
      </context-param>
      
      




      Any suggestions as to what I am missing.

      Thanks

      Louis


        • 1. Re: Lazyload configuration?
          Gavin King Master

          Are you within the scope of the same conversation that originally loaded the object?

          Are you using a Seam-managed persistence context (ie. @In EntityManager em)?

          • 2. Re: Lazyload configuration?
            Louis Coetzee Apprentice

            1. Yes (or rather I think so, quite a steep learning curve thus far ;-)

            2. No. I am using the following :

            
             @PersistenceContext(unitName = "TrySeam1")
             private EntityManager em;
            


            If I use the @In EntityManager em, how do I specify the correct PersistenceContext ?

            L

            • 3. Re: Lazyload configuration?
              Louis Coetzee Apprentice

              Hi all,

              I have reconfigured my web.xml to be the following:

              <context-param>
               <param-name>
               org.jboss.seam.init.managedPersistenceContexts
               </param-name>
               <param-value>TrySeam1</param-value>
               </context-param>
               <context-param>
               <param-name>TrySeam1.persistenceUnitName</param-name>
               <param-value>
               java:/EntityManagerFactories/trySeamEM
               </param-value>
               </context-param>
              
               <context-param>
              
              <!-- is this needed ?? -->
              <param-name>org.jboss.seam.managedPersistenceContexts</param-name>
               <param-value>TrySeam1</param-value>
               </context-param>
              


              And in my Stateful bean I am trying to inject the EntityManager as follows:
              @In(create=true) EntityManager TrySeam1;
              


              Unfortunately I am still missing something... I get the following
              DEBUG [org.jboss.seam.Component] seam component not found: TrySeam1
              
              .

              which is later followed by plenty of exceptions e.g.
              org.jboss.seam.RequiredException: In attribute requires value for component: TrySeam1
              


              Any suggestions ?

              Thanks

              Louis

              • 4. Re: Lazyload configuration?
                Gavin King Master

                It should be org.jboss.seam.core.init.managedPersistenceContexts

                • 5. Re: Lazyload configuration?
                  Gavin King Master

                  OK, my fault, this is wrong in about 12 places in the docs :-(

                  I suck.

                  • 6. Re: Lazyload configuration?
                    Louis Coetzee Apprentice

                    Excellent ! Works like a charm.

                    One interesting thing though:

                    I had to remove the context-param containg the EntityManagerFactories stuff. Only leaving the context-param containing the org.jboss.seam.core.init.managedPersistenceContexts in the web.xml.

                    Thanks for all the inputs.

                    Louis




                    • 7. Re: Lazyload configuration?
                      ryan dewell Novice

                      Hi Louis,

                      If you get a minute, could post the configuration you finally got to work... i.e. your context params? You mentioned something about removing the one about factories?

                      To anyone else:

                      I think I followed everything here, and changed it from the docs to "core". I still can't get seam managed persistence to work though. My latest error is:

                      "No conversation context active"

                      Actually, I get this error A LOT while working with Seam. It really doesn't like it when I use components marked as CONVERSATION that are not in some expected JSF phase. For example, I do a lot of custom stuff in ViewHandler's... and any CONVERSATION beans are basically useless there, throwing the above error. Anything that could be done to remove this limitation in future releases would be great. Or, if I knew how to programmatically start my own conversation, that might work too.

                      Ryan

                      • 8. Re: Lazyload configuration?
                        ryan dewell Novice

                        I made some headway. It seems to be configuring it now:

                        18:05:42,522 INFO [Component:?] Component: bright, scope: CONVERSATION, type: JAVA_BEAN, class: org.jboss.seam.core.ManagedPersistenceContext

                        I also tried moving some of my stuff out of createView in my ViewHandler to afterPhase of RESTORE_VIEW. This appears to solve the "no conversation active" problem. But, a new problem presents itself AFTER the page fully renders.. presumably in some kind of clean up phase:

                        18:12:52,154 ERROR [SeamExceptionFilter:?] could not destroy contexts
                        java.lang.IllegalStateException: No active session context
                        at org.jboss.seam.Seam.isSessionInvalid(Unknown Source)
                        at org.jboss.seam.contexts.Lifecycle.endRequest(Unknown Source)
                        ........
                        java.lang.IllegalStateException: EntityManager is closed
                        at org.hibernate.ejb.EntityManagerImpl.getSession(EntityManagerImpl.java:27)

                        At this point I'm just going to ditch the seam managed PersistenceContext's. Evidentally I'm using this in a way that was not intended.

                        Ryan

                        • 9. Re: Lazyload configuration?
                          Gavin King Master

                           

                          "rdewell" wrote:

                          "No conversation context active"

                          Actually, I get this error A LOT while working with Seam. It really doesn't like it when I use components marked as CONVERSATION that are not in some expected JSF phase. For example, I do a lot of custom stuff in ViewHandler's... and any CONVERSATION beans are basically useless there, throwing the above error. Anything that could be done to remove this limitation in future releases would be great. Or, if I knew how to programmatically start my own conversation, that might work too.Ryan


                          See this thread:

                          http://www.jboss.com/index.html?module=bb&op=viewtopic&t=70250&postdays=0&postorder=asc&start=0

                          Basically, you should not access the model (at least not conversational components) until after the Restore View phase.


                          • 10. Re: Lazyload configuration?
                            Gavin King Master

                             

                            "rdewell" wrote:
                            But, a new problem presents itself AFTER the page fully renders.. presumably in some kind of clean up phase:

                            18:12:52,154 ERROR [SeamExceptionFilter:?] could not destroy contexts
                            java.lang.IllegalStateException: No active session context
                            at org.jboss.seam.Seam.isSessionInvalid(Unknown Source)
                            at org.jboss.seam.contexts.Lifecycle.endRequest(Unknown Source)
                            ........
                            java.lang.IllegalStateException: EntityManager is closed
                            at org.hibernate.ejb.EntityManagerImpl.getSession(EntityManagerImpl.java:27)


                            (1) Are you calling EntityManager.close() manually? If so, don't. It's a managed EM.

                            (2) Have you got a similar problem to this thread:

                            http://www.jboss.com/index.html?module=bb&op=viewtopic&t=69910&start=0

                            (3) Show your debug-level Seam log.

                            • 11. Re: Lazyload configuration?
                              Louis Coetzee Apprentice

                              Hi all,

                              for completeness the appropriate snippets:

                              In faces-config.xml

                              <lifecycle>
                               <phase-listener>
                               org.jboss.seam.jsf.SeamExtendedManagedPersistencePhaseListener
                               </phase-listener>
                              </lifecycle>
                              


                              in web.xml

                              <context-param>
                               <param-name>
                               org.jboss.seam.core.init.managedPersistenceContexts
                               </param-name>
                               <param-value>
                               trySeam1Entity
                               </param-value>
                              </context-param>
                              


                              In my LeagueManagementAction.java (selected snippets)

                              @Stateful
                              @Name("leaguemanagement")
                              @Scope(ScopeType.CONVERSATION)
                              @Conversational(ifNotBegunOutcome="fail")
                              @Interceptor(SeamInterceptor.class)
                              public class LeagueManagementAction implements LeagueManagement,Serializable {
                              @In(create=true) EntityManager trySeam1Entity;
                              .
                              .
                              . @Begin
                              public String findAllTeams(){
                              allTeams = trySeam1Entity.createQuery("from.csir.league.seam.Team").getResultList();
                              logger.info("Retrieved " + allTeams.size() + " teams");
                              return "success";
                              }

                              .
                              .
                              .
                              @End
                              public String cancel() {
                              logger.info("Suppose to end the conversation now");
                              return "finished";
                              }
                              }



                              Hope it helps.

                              Louis