简体   繁体   English

带有QueryDSL的Spring Data JPA,聚合功能的计数问题

[英]Spring Data JPA with QueryDSL, Count issue with aggregate function

I am using Spring Data JPA with QueryDSL and trying to use Sum function in where condition, As i am using pagination so i have to get count first. 我正在将Spring Data JPA与QueryDSL一起使用,并尝试在任何条件下使用Sum函数,因为我正在使用分页,所以我必须先获得计数。 So i have java code like below :- 所以我有如下的Java代码:-

NumberPath<Double> path = entityPath.getNumber("qty", Double.class);
BooleanExpression exp = path.sum().loe(120);        
JPQLQuery countQuery = from(stock).where(exp);
long count = countQuery.count();

its creating query like this :- 它的创建查询是这样的:

select count(stock0_.stock_id) as col_0_0_ from stock stock0_ 
where sum(stock0_.qty)>=120;

and i am getting Error Code: 1111. Invalid use of group function . 并且我收到Error Code: 1111. Invalid use of group function

above query is not working in SQL as well because sum function cant be use with count in where condition. 上述查询在SQL中也不起作用,因为sum函数不能与where条件中的count一起使用。 I have not idea how to deal with such problem when i have to get count first and then fetch the real data. 当我必须先计数然后获取真实数据时,我不知道如何处理此类问题。 Can someone please help me out with what is JPA approach to deal with such issue. 有人可以帮助我解决JPA问题的什么方法。

Please don't suggested @Query annotation because i can not use it. 请不要建议@Query注释,因为我无法使用它。 Due to dynamic filtration requirement. 由于需要动态过滤。

You are using aggregate functions (sum). 您正在使用聚合函数(求和)。 Then you need having() instead where() 然后,您需要Have()代替where()

This example is a really large one. 这个例子是一个很大的例子。 You only need to care about pagination and having elements. 您只需要关心分页和具有元素。

If you got different predicates and a pageRequest object. 如果有不同的谓词pageRequest对象。

Page request example: 页面请求示例:

pageRequest = new PageRequest(
                MI_PAGE, 
                LIMIT, 
                Sort.Direction.valueOf( ASC ), 
                ORDER_FIELD);

EntityQ represents a QueryDSL generated meta-entity. EntityQ代表QueryDSL生成的元实体。

Query example: 查询示例:

new JPAQuery(em).from(entityQ).where( entityQ.id.in(

        new JPASubQuery()
        .from(entity2Q).innerJoin(entity2Q.objEntityQ, entityQ)
            .where(ENTITY, PREDICATE)                           
            .groupBy(entity2Q.objEntityQ.id)
            .having( Wildcard.count.goe(SIZE) )                 
        .list(entity2Q.objEntityQ.id)

    ))
    .offset(pageRequest.getOffset() )
    .limit( pageRequest.getPageSize() ) 
    .orderBy(ORDERS).list(entityQ); 

EDIT 2 More specific for your example : 编辑2 更具体的为您的示例

I usually have a persistence context in my custom repositories: 我通常在自定义存储库中有一个持久性上下文:

@PersistenceContext
EntityManager em

Then, you could create a simple JPAQuery: 然后,您可以创建一个简单的JPAQuery:

new JPAQuery(em)
.from(YOUR_STOCK_QUERYDSL_ENTITY)
.groupBy(YOUR_STOCK_QUERYDSL_ENTITY.field_to_sum)
.having(YOUR_STOCK_QUERYDSL_ENTITY.field_to_sum.sum().as("alias")
.goe(100));

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

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