JPA 2条件获取路径导航 [英] JPA 2 Criteria Fetch Path Navigation

查看:82
本文介绍了JPA 2条件获取路径导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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文档提取返回一个错误的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(),添加a 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条件获取路径导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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