2 Replies Latest reply on Feb 16, 2007 12:18 PM by berkum

    Howto fetch lazy relations before an entity is detached

    scraatz

      Hi,

      is there a standard way to force fetching of lazy initialized related entities?

      We need to detach an object with lazyly loaded relations and access the related objects on the detached object.

      I found that when I access an non-id property of a ...toone relation object within the transaction that loads the main entity, that child-entity is initialized. For ...tomany relations i have to access an non-id property of all children in the collection.

      Is this the way to do it (besides marking the relation as EAGER, which is not an option), or is there a more elegant solution?

      thanks
      Stefan

        • 1. Re: Howto fetch lazy relations before an entity is detached
          jaboj

          It seems like JBOSS has some inexpedients about LAZY loading. We're experiencing the same problem that you describe.

          If I fetch a entity with a LAZY loaded relation into a statefull session bean, then I'm only able to get attributes from the lazy loaded relational object in the same call to the session bean (using CMP). In subsequent call to the sessionbean I will not be able to retrieve attributes from the LAZY loaded relation. This means that when we initialize a statefull bean we'll try to avoid lazy loaded objects by using queries instead - otherwise we'll create a transfer object for the lazy loaded objects on initialization.

          I've have changed a lot of em.find() to EJB queries instead, where the query (using [LEFT] JOIN FETCH) is fetching alle the neccesary relations needed to avoid lazy loading.

          I also think it's a problem that even after loading a LAZY loaded relation the objekt is still a proxy-class type (Ex Company will be of type Company@EnhancerByCG.....) This means that we can't use intanceof to determing the object type of inherited objects in that case.

          • 2. Re: Howto fetch lazy relations before an entity is detached
            berkum

            I do have the same problem, need to iterate through the whole collection to make object available outside the persistence context. I am passing persistent object to web layer. With hibernate, according to the api, you can use Hibernate.initialize() to initialize your Collection. There doesn't seem to be anything planned in jpa for such task. Anyone knows ?