JPA 2 CriteriaQuery中的FetchMode [英] FetchMode in JPA 2 CriteriaQuery
问题描述
我目前正在从Hibernate切换到纯JPA 2(顺便说一句,这比我最初预期的要耗费更多的时间).
到目前为止,我遇到的最大问题是找到一种方法来强制加载懒惰的属性.
使用Hibernate,可以使用criteria.setFetchMode("person", FetchMode.JOIN);
.
完成此操作.
JPA 2有什么方法可以做到这一点?
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?
推荐答案
尝试一下:
CriteriaQuery<Person> c = cb.createQuery(Person.class);
Root<Person> person = c.from(Person.class);
person.fetch("address");
c.select(person);
假设您的Person
实体和Address
实体之间存在一对一的关系.
Assuming there is a one-to-one relationship between your Person
entity and an Address
entity.
这篇关于JPA 2 CriteriaQuery中的FetchMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!