【问题标题】:Spring JPA Criteria Query group by with to_char functionSpring JPA Criteria Query group by with to_char 函数
【发布时间】:2020-02-22 02:42:19
【问题描述】:

我正在尝试按日/月/年分组

Expression<String> expression = criteriaBuilder.function(
                            "to_char", String.class, root.get("reqDate"),
                            criteriaBuilder.parameter(String.class, "dateSegment"));

query.multiselect(
                criteriaBuilder.count(root).alias(FIELD_COUNT), expression.alias("reqDate"))
               .groupBy(expression);
entityManager.createQuery(query)
        .setParameter("dateSegment","MM-YYYY")
        .getResultList();

但这会引发一个错误,即选择字段必须出现在组中,这已经是,我检查了生成的 sql,它在 db 控制台中工作。 但不是 JPA,我做错了什么?

生成的sql

select 
  count(shipment0_.shipment_pk_id) as col_0_0_, 
  to_char(
    shipment0_.requested_delivery_date_time, 
    ?
  ) as col_2_0_ 
from 
  shipment shipment0_ 
group by 
  to_char(
    shipment0_.requested_delivery_date_time, 
    ?
  )

有绑定

2019-10-28 09:20:05,891 TRACE [0.0-8080-exec-6 Wcl5M33fEZTS5iF Wcl5M33fEZTS5iF] - o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [VARCHAR] - [MM-YYYY]
2019-10-28 09:20:05,891 TRACE [0.0-8080-exec-6 Wcl5M33fEZTS5iF Wcl5M33fEZTS5iF] - o.h.type.descriptor.sql.BasicBinder      : binding parameter [10] as [VARCHAR] - [MM-YYYY]

为什么它绑定为两个不同的参数?

日志

org.postgresql.util.PSQLException: ERROR: column "shipment0_.requested_delivery_date_time" must appear in the GROUP BY clause or be used in an aggregate function
  Position: 383
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441) ~[postgresql-42.2.5.jar:42.2.5]

【问题讨论】:

  • 请粘贴java堆栈跟踪错误信息

标签: hibernate group-by spring-data-jpa hibernate-criteria


【解决方案1】:

您的错误是您使用 criteriaBuilder.parameter(String.class, "dateSegment") 作为 DateTime 格式化程序。而不是它,您应该使用cb.literal("MM-YYYY"),然后一切都应该按预期工作。 所以正确的代码如下:

Expression<String> expression = criteriaBuilder.function(
                            "to_char", String.class, root.get("reqDate"),
                            criteriaBuilder.literal("MM-YYYY"));

query.multiselect(
                criteriaBuilder.count(root).alias(FIELD_COUNT), expression.alias("reqDate"))
               .groupBy(expression);
entityManager.createQuery(query)
        .getResultList();

【讨论】:

    【解决方案2】:

    通过使查询看起来像来实现这一点

    select 
      count(shipment0_.shipment_pk_id) as col_0_0_, 
      to_char(
        shipment0_.requested_delivery_date_time, 
        ?
      ) as col_2_0_ 
    from 
      shipment shipment0_ 
    group by 
      2
    

    【讨论】:

      猜你喜欢
      • 2015-02-27
      • 2014-11-27
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 2011-04-24
      • 1970-01-01
      相关资源
      最近更新 更多