6 Replies Latest reply on Jun 29, 2008 5:48 PM by admin.admin.email.tld

    LazyInitializationException

    benitojuarez

      Hello,


      i have an entity (created using seam-gen) with a join field:



           @ManyToOne(fetch = FetchType.LAZY)
      
           @JoinColumn(name = "volio_connection_id")
      
           public VolioConnection getVolioConnection() {
      
                return this.volioConnection;
      
           }
      
      



      If this method is rendered in xhtml, i get the following exception:




      17:17:30,502 ERROR LazyInitializationException could not initialize proxy - no Session
      org.hibernate.LazyInitializationException: could not initialize proxy - no Session
              at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
              at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.jav
      a:111)
              at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializ
      er.java:166)
              at de.its.eai.model.VolioConnection$$javassist2.getVolioItems(VolioConnection$$javassist2.ja
      va)


      I think it has something to do with a wrong entityManager. But i have no idea how to change it in the entity or the xhtml-file.


      thx for help
      BJ

        • 1. Re: LazyInitializationException
          dhinojosa

          You are likely accessing the collection when the persistence session is closed. 

          • 2. Re: LazyInitializationException
            benitojuarez

            ok, but how can i open the session in my template?

            • 3. Re: LazyInitializationException
              dhinojosa

              Your template? 


              There's not construct in Seam called a template.  Unless you use seam gen's template.xhtml, but I don't think you are referring to that.


              Your persistence session is usually defined by the scope of your entity and the scope of your session bean.  So what are those scopes?

              • 4. Re: LazyInitializationException
                gjeudy

                Are you using SMPC ? (Seam managed persistence context)


                It sounds like you are not familiar with persistence contexts, I suggest you read up relevant docs on this.
                See: Seam managed persistence contexts


                If you use SMPC then your persistence context is scoped to the conversation by default. What you are attempting to do is lazy initialize an entity that is no longer managed by it's persistence context (it is in a detached state) because the conversation in which the entity was originally loaded has ended.


                When Seam ends a conversation it will also close the SMPC.

                • 5. Re: LazyInitializationException
                  benitojuarez

                  Hello,



                  It sounds like you are not familiar with persistence contexts, I suggest you read up relevant docs on this.


                  it's the truth, i'm not familiar to the persistence contexts yet. So please be patience.


                  I solved the problem using



                    @In
                  
                     private EntityManager entityManager;



                  instead of @PersistenceContext.


                  thx for the link
                  BJ

                  • 6. Re: LazyInitializationException
                    admin.admin.email.tld

                    It's recommended to use @In to inject the EntityManager in Seam components.


                    You could also use the extended PersistenceContext:


                    @PersistenceContext(type=PersistenceContextType.EXTENDED)
                    EntityManager em;



                    The dvdstore example in the distro is using the above code to inject the EntityManager.  Most likely you were using the default Transaction type.


                    from Hibernate docs:


                    You can also use an extended persistence context. This can be combined with stateful session beans, if you use a container-managed entity manager: the persistence context is created when an entity manager is retrieved from dependency injection or JNDI lookup , and is kept until the container closes it after the completion of the Remove stateful session bean method. This is a perfect mechanism for implementing a long unit of work pattern. For example, if you have to deal with multiple user interaction cycles as a single unit of work (e.g. a wizard dialog that has to be fully completed), you usually model this as a unit of work from the point of view of the application user, and implement it using an extended persistence context.
                    see link