3 Replies Latest reply on Aug 29, 2007 3:28 PM by wquraishi

    Lazy collection load

    sierras

      In order to load a lazy collection of a detached entity I've a method in a session bean that does the work and returns the same instance of the object but with the collection loaded....

      public Country RefreshCountryCollection(Country country)
      {
      em.merge(country);
      country.getChildrenOrganizations();
      return country;
      }

      Is this the rigth way of doing so??? Or there is a better way of doing so?

      Thanks in advance!

        • 1. Re: Lazy collection load
          sierras

          Obviouly is not the way because it does'nt work. I get an Exception

          org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: entities.Country.organizations, no session or session was closed
          at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
          at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
          at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
          at org.hibernate.collection.PersistentBag.size(PersistentBag.java:222)
          at beans.CountryBean.doRefreshWithOrganizations(CountryBean.java:142)
          ....


          Any idea of what to do?

          Thanks.

          • 2. Re: Lazy collection load
            abl

            you have to use the entity returned from merge:

            public Country RefreshCountryCollection(Country country)
            {
            country = em.merge(country);
            country.getChildrenOrganizations();
            return country;
            }

            • 3. Re: Lazy collection load
              wquraishi

              One thing I discovered is if you override the toString() method and output the relationships, it'll force the container to load the relationship related fields. I'm thinking it accomplishes the same thing as fetchtype.eager but i've had more luck with this. Plus it works dual fold since I use the toString() method in conjunction with facesMessages to display what the object coming back is.