JPA 2 CriteriaQuery中的FetchMode [英] FetchMode in JPA 2 CriteriaQuery

查看:104
本文介绍了JPA 2 CriteriaQuery中的FetchMode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆