Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FetchMode in JPA 2 CriteriaQuery

Tags:

jpa-2.0

I'm currently in the process of switching from Hibernate to pure JPA 2 (which by the way turned out to be much more time consuming than I initially expected).
The biggest problem I'm having so far is finding a way to force eager loading of lazy properties.
With Hibernate this was done using: criteria.setFetchMode("person", FetchMode.JOIN);.
Is there any way to do this with JPA 2?

like image 729
Johannes Avatar asked Dec 08 '10 14:12

Johannes


1 Answers

Try this:

CriteriaQuery<Person> c = cb.createQuery(Person.class);
Root<Person> person = c.from(Person.class);
person.fetch("address");
c.select(person);

Assuming there is a one-to-one relationship between your Person entity and an Address entity.

like image 56
Jim Tough Avatar answered Oct 26 '22 21:10

Jim Tough