简体   繁体   English

JPA 标准查询以查找每个唯一名称的 MAX ID

[英]JPA Criteria Query to find the MAX ID for each unique name

EDIT: Apparently my prior question was not sufficient so I will try to make it more clear with this try.编辑:显然我之前的问题还不够,所以我会尝试通过这次尝试使其更清楚。

I have a set of rows in the table, selected by predicate p1 below each of which have a unique value in the id column.我在表中有一组行,由下面的谓词p1选择,每行在id列中都有一个唯一的值。 The pathName column may have multiple instances of the same value. pathName列可能有多个相同值的实例。 What want is to return only the entities that have a distinct pathName with the highest value id .想要的是只返回具有最高值id的不同pathName的实体。

 CriteriaBuilder builder = em.getCriteriaBuilder();
 CriteriaQuery<TextDocument> query = builder.createQuery(TextDocument.class);
 Root<TextDocument> root = query.from(TextDocument.class);

 // descending ids, and group similar names together

 query.orderBy(builder.desc(root.get(TextDocument_.id)));
 query.groupBy(root.get(TextDocument_.pathName));

 // filter

 Predicate p1 = builder.equal(root.get(TextDocument_.parent), rootdoc);
 Predicate p2 = builder.equal(root.get(TextDocument_.id), builder.max(root.get(TextDocument_.id)));
 query.where(p1, p2);

The predicate p1 selects the documents that all have the same parent document and this of course works as long as it is by itself.谓词p1选择所有具有相同父文档的文档,只要它本身就可以工作。

Predicate p2 is all wrong, however.然而,谓词p2全错了。 Adding it causes an exception ERROR 42903: Invalid use of an aggregate function.添加它会导致异常错误 42903:聚合函数的使用无效。 . .

The solution to a similar query problem is shown here in SQL but I want to do it using JPA Criteria Query.以类似的查询问题的解决方案显示这里在SQL,但我想用JPA标准查询做到这一点。 I can't figure out how to make the nested SELECT syntax work.我不知道如何使嵌套的 SELECT 语法起作用。

To reiterate the problem with my own example I have the following data set为了用我自己的例子重申这个问题,我有以下数据集

id  parent  pathName  
--- ------- --------- 
101     22  alpha
130     22  beta
250     22  charlie
251     22  alpha
339     22  beta
400     22  alpha
401     22  delta

The correct result (for parent=22) would be:正确的结果(对于 parent=22)将是:

id  parent  pathName  
--- ------- --------- 
250     22  charlie
339     22  beta
400     22  alpha
401     22  delta

Can anyone help me finish this query?谁能帮我完成这个查询?

you can do this你可以这样做

SELECT * from [tableName] t1
WHERE not exists(SELECT * from [tableName]t2 where t1.name = t2.name and t2.id > t1.id)

Using Reza example for sql:使用 Reza 的 sql 示例:

SELECT * from [tableName] t1
WHERE not exists(SELECT * from [tableName]t2 where t1.name = t2.name and t2.id > t1.id)

And assuming the existing Root< TableEntity > root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder, you can write the JPA Criteria:并假设现有的 Root<TableEntity> 根、CriteriaQuery<?>criteriaQuery、CriteriaBuildercriteriaBuilder,您可以编写 JPA Criteria:

Subquery<TableEntity> subQuery = criteriaQuery.subquery(TableEntity.class);
Root<TableEntity> subQueryRoot = subQuery.from(TableEntity.class);
subQuery.select(subQueryRoot)
        .where(criteriaBuilder.and(
                criteriaBuilder.equal(subQueryRoot.get(TableEntity_.name), root.get(TableEntity_.name)),
                criteriaBuilder.greaterThan(subQueryRoot.get(TableEntity_.id), root.get(TableEntity_.id))
        ));
predicates.add(criteriaBuilder.not(criteriaBuilder.exists(subQuery)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM