解决 JPA2.1 支持 group by 多个字段

可以使用 Hibernate API 中的 @Formula 解决 group by 中多个字段的问题,代码如下:

maven 依赖

        
            org.hibernate
            hibernate-core
            5.0.12.Final
            
                
                    org.apache.geronimo.specs
                    geronimo-jta_1.1_spec
                
            
        
        
            org.hibernate
            hibernate-entitymanager
            5.0.12.Final
            
                
                    org.apache.geronimo.specs
                    geronimo-jta_1.1_spec
                
            
        

entity 对象中添加 concat 属性

    @Formula("concat(date, receive_way)")
    private String concated;

repo 中基于 concated 字段进行分组

        JPAQuery query = new JPAQuery(em);
        QSettleAccount settleAccount = QSettleAccount.settleAccount;

        return query.select(
                settleAccount.date,
                settleAccount.receiveWay,
                settleAccount.settleAmount.sum(),
                settleAccount.procedureFee.sum())
                .from(settleAccount)
                .groupBy(settleAccount.concated)
                .having(predicate)
                .offset(0)
                .limit(10)
                .fetchResults();

以上方式已经验证通过

你可能感兴趣的:(解决 JPA2.1 支持 group by 多个字段)