5 Replies Latest reply on Mar 12, 2012 4:38 AM by yuan qixun

    Help for the org.hibernate.LazyInitializationException

    yuan qixun Newbie

      hello every one, please help me for the exception.

       

      1. I have two One2One entity: OneA.class and OneB.class and the load is LAZY

      2. A conversation scope bean to fetch the one object from DB.

       

      3.1 page1.xhtml

       

      <h:outputText value="oneA name:#{bean.oneA.name}" />
      <br/>
      <p:commandLink value="open page2" onclick="window.open('/page2.seam?cid=#{conversation.id}')"/>
      
      

       

      3.2 page2.xhtml

       

      <h:outputText  value="oneB name:#{bean.oneA.oneB.name}" />
      
      

       

      as such above, it's so easy, but when open page2, there will be a exception as follow:

       

      Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
                at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149) [hibernate-core-4.0.0.CR2.jar:4.0.0.CR2]
                at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195) [hibernate-core-4.0.0.CR2.jar:4.0.0.CR2]
                at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:180) [hibernate-core-4.0.0.CR2.jar:4.0.0.CR2]
      
      

       

      any one know the reason? any wrong for me?

        • 1. Re: Help for the org.hibernate.LazyInitializationException
          zeeman Novice

          Have you configured Seam persistence like mentioned in the documentation? Let seam manage the entity manager for you. If you inject with @Inject the entityManager you should not have this issue.

          • 2. Re: Help for the org.hibernate.LazyInitializationException
            yuan qixun Newbie

            you can see my configure:

             

            1. I have one produce class

             

            @ConversationScoped
            public class PersistenceManager implements Serializable{
                @Produces
                @CRM
                @PersistenceUnit(unitName="crm")
                EntityManagerFactory crmEMFactory;
                
                @Produces
                @Dependent
                @CRM
                @PersistenceContext(unitName="crm",type=PersistenceContextType.EXTENDED)
                EntityManager crmEntityManager;
            }
            
            

             

            2. in my service bean, I inject the em:

             

            @Named("crm.room.roomSVC")
            
            @ConversationScoped
            public interface RoomSVC {
            ......
            }
            
            public class RoomSVCImpl implements Serializable, RoomSVC {
                      @Inject
                      @CRM
                      EntityManager em;
                 ....... do some database access.....
            }
            

             

            3. in my action bean used by xhtml page

             

            @ConversationScoped
            @Named("crm.room.roomZiLiaoDetailAction")
            public class RoomZiLiaoDetailAction implements Serializable {
                      @Inject
                      RoomSVC roomSVC;
                    
                    private Room room;//getter and setter omit
            
                      @PostConstruct
                      public void afterCreate() {
                                roomUuid = roomUuidReq.get();
                                if (StringUtils.isNotEmpty(roomUuid)) {
                                          log.infov("The room uuid:{0}", roomUuid);
                                          room = roomSVC.findRoomById(roomUuid);
                                } else {
                                          // TODO 需要新增吗,否则应该抛出异常?
                                }
                      }
            
            }
            
            

             

            4. The room entity have Many2One attribute

             

            @Entity
            @Table(name = "CRM_ROOM")
            public class Room {
            ....
            @ManyToOne(fetch = FetchType.LAZY)
                      @JoinColumn(name = "HXBH", referencedColumnName = "BH")
                      private HouseType houseType;
            }
            

             

            5. two page call the RoomZiLiaoDetailAction bean

            page1.xhtml

            <h:outputText value="room:#{crm.room.roomZiLiaoDetailAction.room.name}" />
            
            

            page2.xhtml

             

            <h:outputText  value="houseType:#{crm.room.roomZiLiaoDetailAction.room.houseType.name}" />
            
            

             

            over, and you will see the exception.

            • 3. Re: Help for the org.hibernate.LazyInitializationException
              zeeman Novice

              you're missing few things.

               

              This is how I produce my EMF. I don't produce EM as Seam knows how.

               

              //Don't put any scope here

              public class PersistenceConfiguration {

               

                        @Produces

                        @ExtensionManaged

                        @ConversationScoped

                        @PersistenceUnit

                        private EntityManagerFactory conversationEMF;

               

              }

               

               

              On RoomZiLiaoDetailAction , be sure you're starting and end the conversation correctly.

              1 of 1 people found this helpful
              • 4. Re: Help for the org.hibernate.LazyInitializationException
                yuan qixun Newbie

                I don't produce EM as Seam knows how.

                But,how to inject the entityManager in your action bean? please give a example, thanks.

                • 5. Re: Help for the org.hibernate.LazyInitializationException
                  yuan qixun Newbie

                  Yes,you are right, I followed your guide, and no this exception!