美文网首页
解决 JPA2.1 支持 group by 多个字段

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

作者: aaron_ouyang | 来源:发表于2017-11-14 22:37 被阅读22次

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

maven 依赖

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.12.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-jta_1.1_spec</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.12.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-jta_1.1_spec</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

entity 对象中添加 concat 属性

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

repo 中基于 concated 字段进行分组

        JPAQuery<?> query = new JPAQuery<Void>(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 多个字段

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

  • group by 两个字段

    sql:常用:group by 多个字段_不花的花和尚的博客-CSDN博客_group by 多个字段

  • 组合数据

    concat 组合多个字段 GROUP_CONCAT # group by 之后组合单个字段

  • MySql查询-分组

    group by group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组 group b...

  • 分组查询

    分组查询 group by group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组 gr...

  • SQL应注意小知识点(仅供个人学习)

    一、 分组 group by 多个字段进行分组时,分组字段全部相同时才能进行分组注意多个分组不是一个字段分组之后另...

  • 分组筛选的异同

    语法 特点: GROUP BY后跟分组函数查询的字段 分组可以按单个字段也可以多个字段 案例 分组前查询 查询班级...

  • 取最新的记录

    方法1:group by 字段问题通过join解决 方法2:over partition by select * ...

  • mysql:group by,order by

    mysql:group by,order by order by order by是用于支持字段的关键字 表结构如...

  • 24《MySQL 教程》 GROUP BY分组

    本小节介绍如何对查询结果使用 GROUP BY 分组,GROUP BY 分组是对指定一个或多个字段分组,使用分组可...

网友评论

      本文标题:解决 JPA2.1 支持 group by 多个字段

      本文链接:https://www.haomeiwen.com/subject/injqvxtx.html