JPA 2 Criteria Fetch Path Navigation(JPA 2 Criteria Fetch Path Navigation)
问题描述
使用 JPA 2 Criteria Join 方法,我可以执行以下操作:
With JPA 2 Criteria Join method I can do the following:
//Join Example (default inner join)
int age = 25;
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Team> c = cb.createQuery(Team.class);
Root<Team> t = c.from(Team.class);
Join<Team, Player> p = t.join(Team_.players);
c.select(t).where(cb.equal(p.get(Player_.age), age));
TypedQuery<Team> q = entityManager.createQuery(c);
List<Team> result = q.getResultList();
我怎样才能用 fetch 方法做同样的事情,我希望 Fetch 接口有路径导航的 get 方法,但它没有:
How can I do the same with fetch method, I expected that Fetch interface had get method for path navigation but it doesn't:
//Fetch Join Example
int age = 25;
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Team> cq = cb.createQuery(Team.class);
Root<Team> t = cq.from(Team.class);
Fetch<Team,Player> p = t.fetch(Team_.players);
cq.where(cb.equal(p.get(Player_.age), age)); //This leads to compilation error there is no such method get in interface Fetch
TypedQuery<Team> q = entityManager.createQuery(cq);
List<Team> result = q.getResultList();
根据 Hiberante Documentation fetch 返回一个错误的 Join 对象.http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/querycriteria.html#querycriteria-from-fetch
According to Hiberante Documentation fetch returns a Join object which is wrong. http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/querycriteria.html#querycriteria-from-fetch
推荐答案
同意你关于该方法的观点,以及你希望它允许你所说的事实.另一种选择是
Agree with you about that method, and the fact that you would expect it to allow what you say. Another option would be
Join<Team, Player> p = t.join(Team_.players);
t.fetch(Team_.players);
c.select(t).where(cb.equal(p.get(Player_.age), age));
即执行一个 join()
,为其添加一个 fetch()
,然后使用该连接.这是不合逻辑的,只会增加 JPA 标准的不雅性,但无论如何,应该是一种解决方法
i.e do a join()
, add a fetch()
for it, and then make use of the join. This is illogical and only adds to the inelegant nature of JPA Criteria, but anyway, ought to be a workaround
这篇关于JPA 2 Criteria Fetch Path Navigation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JPA 2 Criteria Fetch Path Navigation
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01