10 Replies Latest reply on Mar 2, 2006 4:32 PM by Emmanuel Bernard

    How to create lazy collections

    Pepe Mosca Newbie

      Hi,

      I am trying to initialize lazily a collection with FetchMode.LAZY. I have a method in a stateful bean with executes a query with the injected entity manager. When I use a lazy relationship between to entities I get this exception:

      org.hibernate.LazyInitializationException: failed to lazily initialize a collection - no session or session was closed
       at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:179)
       at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:49)
       at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:231)
       at com.ebiointel.test.client.UserTest.main(UserTest.java:26)


      I don't see how can I take control of the session, as I do when using SessionFactory in hibernate...

      Any documentation and hints will be greatly appreciated,

      Best regards

        • 1. Re: How to create lazy collections
          Florian Fray Newbie

          Hi Bruno,

          got the same problem! Havent found any hint on that.
          Perhaps the provided version of Hibernate is a bit buggy.
          I'll watch this topic and we'll see if somebody got a solution.

          Greetz

          Florian

          • 2. Re: How to create lazy collections
            Bill Burke Master

            How are you interacting with this object. If the object has been detached from persistence storage, then it would no longer be able to resolve the relationship.

            The object would be detached if you are accessing it outside the transaction it was created, found, or merged.

            You need to reattach the object by calling em.merge() or by refinding it.

            • 3. Re: How to create lazy collections
              Bill Burke Master

              FYI, this is smilar behavior to EJB 2.1. Relationships must be accessed within the context of a transaction.

              • 4. Re: How to create lazy collections
                Pepe Mosca Newbie

                But then how can I access the children of the lazy collection if I am outside the bean that contains the injected Entity Manager? Is there the possibility to get the EntityManager from outside the bean directly from the context using jndi? If so, how? I have tried several lookups without success...

                Thanks,

                • 5. Re: How to create lazy collections
                  Bill Burke Master

                  You mean in a remote client? No...EntityManager cannot be access remotely.

                  On the server side you'll have to pre fetch the relationship in your Query or traverse the relationship while the object is still attached to persistence management.

                  Although the behavior is quite different, this limitation is no different than EJB 2.1. Something like this cannot be supported because these are POJOs and need to act like POJOs.

                  Bill

                  • 6. Re: How to create lazy collections
                    Pepe Mosca Newbie

                    Thanks Bill, I take the point. I realize that my mind is too hibernate-shaped and I want to control sessions and factories everywhere. I will just have to adapt my logic to this structure...

                    • 7. Re: How to create lazy collections
                      Bill Burke Master

                      No no...I think I may be misinterpreting your posts here. You should think in Hibernate...Eventually, once it is cleared up, you'll be able to access the EntityManager anywhere.

                      I may be wrong, but your current usage of LAZY loading wouldn't work with a pure Hibernate application as well unless you reattached the object on the remote client with a local Session. This is what I was trying to get at when I responded to you.

                      Huh...Am I making sense at all?

                      Bill

                      • 8. Re: How to create lazy collections
                        Pepe Mosca Newbie

                        Hehe, don't worry, one thing I have to improve eventually is my ability to communicate. All that I was trying to do figure is how to access the EntityManager, as I do with SessionFactories (configured in jboss-service.xml files). For instance, I can get a SessionFactory from a servlet using jndi and I was trying to do something similar with the EntityManager... I don't know if this is the way, because all that I want to do is to create a local session (in a servlet, for instance) to access the ejb3/hibernate3 entities that I have in a ejb3 file somewhere in the server...
                        I don't know if I explain myself ok,

                        Thanks!

                        Bruno

                        • 9. Re: How to create lazy collections
                          Diederick Verweij Newbie

                          I ran into this same problem. (LazyInitializationException: failed to lazily initialize a collection - no session or session was closed ..)

                          The solution for my problem was the removal of a targetEntity at one end of an association.

                          This example describes the problem cause:

                          object A {
                          @OneToMany(mappedBy="a", targetEntity=A.class)
                          List as;
                          }

                          Object B {
                          @ManyToOne(mappedBy="b", targetEntity=B.class)
                          }

                          The targetEntity givven in B (reffering to A) causes the Exception.
                          It probably causes a circular fetching problem (or whatever..)

                          Diederick Verweij.

                          • 10. Re: How to create lazy collections
                            Emmanuel Bernard Master

                            @ManyToOne(mappedBy="b", targetEntity=B.class)

                            never ever compiled