I have a class where two fields are annotated with @Basic(fetch = FetchType.LAZY) due to them containing quite long Strings and me loading ALL entities of the database on application startup (swing app). Yet they don't seem to be fetched lazily.
This class should hopefully demonstrate the problem:
import javax.persistence.*; @Entity public class Sample { private String title; @Basic(fetch = FetchType.LAZY) private String description; @Basic(fetch = FetchType.LAZY) private String code; public Sample() { } public Sample(String title, String description, String code) { this.title = title; this.description = description; this.code = code; } //getters and setters public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/sample.odb"); EntityManager em = emf.createEntityManager(); Sample sample = new Sample("Title", "long'ish description", "really LONG code string"); em.getTransaction().begin(); em.persist(sample); em.getTransaction().commit(); em.flush(); em.close(); emf.close(); //Close everything and reopen to make sure no cache of the description / code is left behind emf = Persistence.createEntityManagerFactory("$objectdb/db/sample.odb"); em = emf.createEntityManager(); sample = em.createQuery("select s from Sample s", Sample.class).getResultList().get(0); em.detach(sample); //just to be sure em.close(); emf.close(); System.out.println(sample.title); System.out.println(sample.description); System.out.println(sample.code); } }
I expect a LazyInitializationException on the last 2 println statements, but it just prints the content, meaning both code and description properties must have been loaded on the query, ignoring the fetch = FetchType.LAZY hint.
Am I doing something wrong?
(I am using objectdb-2.3.6_10)
tia,
Wim