带有子选择的JPA查询

时间:2019-10-25 08:43:56

标签: jpa

我想表达以下查询

select * from(
            select pI, max(pAs) as pAs from mytable
            and pAs>=?1 and pAs<=?2
            and pI like 'DE%%'
            and pE like ?6
            group by pI
            ) as x
            inner join mytable as a
            on a.pI=x.pI
            and a.pAs=x.pAs

使用休眠中的criteriabuilder。我没有成功,因此到目前为止使用了nativeQuery。

但是,问题的症结(我无法解决)似乎是我需要在subselect中返回两个属性。我发现的所有示例仅返回一个属性。

这真的是对jpa / hibernate的限制,还是有一种方法可以做我想要的? 任何帮助/指针,表示赞赏。

1 个答案:

答案 0 :(得分:0)

您未提供有关您实体的任何信息。因此,我怀疑您有Mytable个实体类,并且它有Long个字段pAs

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

Subquery<Long> subquery = query.subquery(Long.class); // max(pAs)
Root<Mytable> subRoot = subquery.from(Mytable.class);

Predicate predicate1 = 
         builder.equal(root.get("pI"), subRoot.get("pI"));

Predicate predicate2 = 
         builder.greaterThan(subRoot.get("pAs"), 0);

Predicate predicate3 = 
         builder.lessThan(subRoot.get("pAs"), 100);

//There is a simple example. But you can add as many predicates as you need
Predicate fullPredicate = 
         builder.and(predicate1, predicate2, predicate3);

Predicate havingPredicate = 
         builder.equal(root.get("pAs"), builder.max(subRoot.get("pAs")));

subquery.select(builder.max(subRoot.get("pAs"))).where(fullPredicate)
    .groupBy(subRoot.get("pI")).having(havingPredicate);

query.select(root).where(builder.exists(subquery));

List<Mytable> result = entityManager.createQuery(query).getResultList();
相关问题