I have been experimenting with JPA a bit lately to try to understand the whole structure a bit more. I use Eclipselink as a JPA provider.
I have two objects with @OneToMany relation (one person has many addresses), which is lazy.
When I download a user object, disconnect it, and then try to access (non-loaded) addresses ... it works like a charm. When debugging, I see that the database query is executed when the size() method of the address list is executed.
I do not understand why this works. I would expect some kind of exception. I read a lot about jpa, etc. In recent days (i.e. this link ), but everything indicated that it should not work.
Can someone explain why this works?
@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); } }
The resulting log will be
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}]}}
source share