用于加入子查询的JPA CriteriaBuilder

时间:2016-02-02 08:22:46

标签: sql hibernate jpa

我有类似这样的SQL语句:

SELECT * FROM person inner join (select max(validityId) as maxID from person group by personId) maxID on maxID.maxID = person.validityid;

因此它为personID提供了“不同”的行:

如果我有这样的表:

|  personID  |  validitID | value   |
-------------------------------------------
|    1       |    10      | 400     |
|    1       |    11      | 500     |
|    1       |    12      | 600     |
|    2       |    13      | 700     |
|    2       |    14      | 800     |
|    2       |    15      | 900     |
|    4       |    16      | 1000    |

它将返回

|  personID  |  validitID | value   |
-------------------------------------------
|    1       |    12      | 600     |
|    2       |    15      | 900     |
|    4       |    16      | 1000    |

现在我尝试通过JPA CriteriaBuilder来做到这一点。

我的第一个想法是子查询:

final CriteriaBuilder cb = this.em.getCriteriaBuilder();
final CriteriaQuery<Person> cq = cb.createQuery(Person.class);
final Root<Person> root = cq.from(Person.class);
cq.select(root);

final Subquery<Long> subquery = cq.subquery(Long.class);
final Root<Person> validityIDSQ = subquery.from(Person.class);
subquery.select(validityIDSQ.get(Person_.validityId));
subquery.groupBy(validityIDSQ.get(Person_.personId));

cb.where(cb.in(root.get(Person_.validityId)).value(subquery));

但这会产生错误

 ERROR: column "person1_.validityid" must appear in the GROUP BY clause or be used in an aggregate function

这是怎么回事?

马尔科

1 个答案:

答案 0 :(得分:6)

我认为解决方案比看起来简单。您忘记在CriteriaBuilder子查询中包含A inst =new A(); ++inst.agressivePoints; 。以下代码执行您要查找的查询。

cb.max ()

此代码将创建以下查询:

final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Person> cq = cb.createQuery(Person.class);
final Root<Person> root = cq.from(Person.class);
cq.select(root);

final Subquery<Integer> subquery = cq.subquery(Integer.class);
final Root<Person> validityIDSQ = subquery.from(Person.class);
subquery.select(cb.max(validityIDSQ.get(Person_.validityID)));
subquery.groupBy(validityIDSQ.get(Person_.personID));

cq.where(cb.in(root.get(Person_.validityID)).value(subquery));

我认为你正在使用Postgres。没有select person0_.id as id1_0_, person0_.personID as personID2_0_, person0_.validityID as validity3_0_, person0_.value as value4_0_ from person person0_ where person0_.validityID in ( select max(person1_.validityID) from person person1_ group by person1_.personID) 它会生成您引用的错误,因为您在不使用聚合函数的情况下使用GroupBy。我在Postgres和MySQL上测试过它。在两者上都像是一种魅力。