JPA call method for lazy (not loaded) collection when disconnected does not work as expected in Eclipselink

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}]}} 
+5
source share
1 answer

As described here , detach removes Entity from the context so that it is no longer managed. Since the context is still accessible, but unused lazy collections can be retrieved if necessary, and EclipseLink considers this more valuable than the exception. This is considered a feature of EclipseLink and is allowed by the JPA specification, although other providers do not allow default behavior.

+1
source

Source: https://habr.com/ru/post/1237838/


All Articles