title: JPA call method on lazy (not loaded) collection when detached not working as expected in Eclipselink
body: <p>lately I experimented a little with JPA to try to understand the whole framework a little more. I am using Eclipselink as JPA provider.</p>
  
  <p>I have two entities with a <code>@OneToMany</code> relationship (one person has many addresses) that is lazy loaded. </p>
  
  <p>When I load a person entity, detach it and then try to access the (not loaded) addresses... it works as a charm. When debugging I can see that a database query is executed when executing the <code>size()</code> method of the address list.</p>
  
  <p>I do not understand why that works. I would expect some kind of exception. I have read quite a few about jpa and the like in the last days (i. e. <a href="https://stackoverflow.com/a/20453458">this link</a>) but everything pointed me to the conclusion that it should not work.</p>
  
  <p>Can anyone please explain why that works?</p>
  
  <pre><code>@Stateless
  public class Test {
      @PersistenceContext(unitName="TestPU") EntityManager em;
  
      public void test() {
          Person person = em.find(Person.class, 1);
  
          System.out.println(person);
  
          System.out.println("em.contains(person): " + em.contains(person);
  
          em.detach(person);
  
          System.out.println("em.contains(person): " + em.contains(person);
  
          person.getAddresses().size();
  
          System.out.println("em.contains(person): " + em.contains(person);
  
          System.out.println(person);
  
      }
  }
  </code></pre>
  
  <p>The resulting log would be</p>
  
  <pre><code>DEBUG: SELECT ... from PERSON WHERE (id = ?)
  Person{id=1, name=Test, addresses={IndirectList: not instantiated}}
  em.contains(person): true
  em.contains(person): false
  DEBUG: SELECT ... FROM ADDRESSES where (address_fk = ?)
  em.contains(person): false
  Person{id=1, name=Test, addresses={[Address{id=10, city=Testcity}]}}
  </code></pre>