title: Eclipselink Lazy Loading
body: <p>I am running a test on eclipselink JPA 2, to determine how lazy loading of a collection is working. I have assumed, the if you load an entity, then all eager elements are loaded first of all, then, within the JPA session, the lazy elements are loaded when you ask for them, or touch them (refer to them in some way, like getting the size of a lazy collection). The problem I have is this: when I detach the entity from the session, the lazy collection is loaded, and is available, even though I didn't ask for it?? I either mis-understand the JSR, or this is normal behaviour in eclipselink. From using hibernate, I know this does not happen.</p>
  
  <pre><code>EntityManager em = emf.createEntityManager();
          AloadTest at1 = em.find(AloadTest.class, pkLazy);
  
          serializeObject(at1,"InSessionLazy");
  
          em.detach(at1);
          em.close();
  </code></pre>
  
  <p>If I run this in a debug, and watch my lazy elements, I can see they are not loaded when I access my 'at1' object, but the minute I step over the em.detach(at1) line, the lazy entities are loaded.</p>
  
  <p>I have a defined lazy collection in my AloadTest C.D. as such:...</p>
  
  <pre><code>@OneToMany(fetch = javax.persistence.FetchType.LAZY, cascade = CascadeType.PERSIST, /*, cascade = CascadeType.ALL, */mappedBy = "aloadtest")
      public Set&lt;CloadLazyMultitest&gt; getCloadLazyMultitest() {
          return cloadLazyMultitest;
      }
      public void setCloadLazyMultitest(Set&lt;CloadLazyMultitest&gt; cloadLazyMultitest) {
          this.cloadLazyMultitest = cloadLazyMultitest;
      }
  </code></pre>
  
  <p>Thanks in advance, but I do not think my lazy collection should be loaded if I havent asked for it.</p>
  
  <p>EDIT: I did your test James, and your are correct about the indirection test:</p>
  
  <pre><code>logger.info(" ARE WE LAZY LOADED BEFORE :"+((IndirectSet)at1.getCloadLazyMultitest()).isInstantiated()); 
  
          em.detach(at1);
  
          logger.info(" ARE WE LAZY LOADED AFTER :"+((IndirectSet)at1.getCloadLazyMultitest()).isInstantiated()); 
  </code></pre>
  
  <p>And the output to my logger:</p>
  
  <pre><code>17:14:16.707 [main] INFO  c.c.t.j.t.JpaI3EagerAndLazyLoadingTest -  ARE WE LAZY LOADED BEFORE :false
  </code></pre>
  
  <p>17:14:16.723 [main] INFO  c.c.t.j.t.JpaI3EagerAndLazyLoadingTest -  ARE WE LAZY LOADED AFTER :true</p>
  
  <p>My point is? why am I getting lazy collections loaded, I dont ask for the collection, just the parent entity. What would happen if I had a chain of 2 or 10 collections, each annotated as a lazy collection? I think it would be quite an overhead. Its never been an issue before with Hibernate, but as eclipselink is now the JPA refernce, I have to build a solution based on this.</p>
  
  <p>Surely its ok to detach an object, to make it 'dirty', do some processing and re-attach to a new session. I am thinking more in a 'long conversation' environment, probably stateless session beans and a web front? </p>