I am trying examples for fetchType.Lazy
, however while debugging the code, it seems that fetchType.Lazy
is not working.
Entity bean: Addres
The problem is that when using in the @Basic
annotation the application server can/may decide on his own, when it is better to fetch the data. As documented here
The EAGER strategy is a requirement on the persistence provider runtime that the value must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime.
@OneToMany
-like annotations have according to the documentation the same problem, but in that case it much more likely that the JPA provider will consider the FetchType hint.
On the other hand, you could try with a big data field, like
@Lob
@Basic(fetch=FetchType.LAZY)
private byte[] document;
to see whether something changes (although again, your application server can decide to fetch always the field).
In order to check whether a collection field was loaded I would either check the database query logs or do the following:
@PersistenceContext private EntityManager em;
.....
Person person = em.find(Person.class, 1L);//Person entity has a OneToMany relationship to entity Address
em.detach(person);
person.getAddresses().size();//if the Address are now not fetched, it the call should throw an error
Ensure you have weaving enabled.
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving
To initialize laze in JPA, you need to invoke a jar library and start it, if it is with maven or manual por example , if you need laze, use jar in maven jar de.empulse.eclipselink More info http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Static_Weaving#Use_the_Maven_plugin
If you don use maven , you can making manually enabled the jar