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?
Asked
Active
Viewed 1.6k times
14
![](../../users/profiles/353679.webp)
Johannes
- 267
- 1
- 2
- 5
1 Answers
18
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.
![](../../users/profiles/346112.webp)
Jim Tough
- 13,464
- 23
- 66
- 90
-
1also if you want to retrieve hierachy elements (the root special case) you should have to use a left join like this. `CriteriaQuery
criteria = cb.createQuery(JerarquiaOrganizacional.class); Root – dennisbot Jan 28 '16 at 19:18jerarquiaOrg = criteria.from(JerarquiaOrganizacional.class); jerarquiaOrg.fetch("jerarquiaOrganizacional", JoinType.LEFT);`