Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

jpa 2.0 - FetchMode in JPA 2 CriteriaQuery

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...