postgresql - 在 JPA 中按截断日期分组

标签 postgresql jpa hql specifications

我需要帮助!

我需要为

建立规范
SELECT date_trunc('day', start_time)
FROM Example
GROUP BY date_trunc('day', start_time)

(PostgreSQL)

我有代码:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Object[]> query = cb.createQuery(Object[].class);

Root<Example> root = query.from(Example.class);

Expression<Date> exp = cb.function("date_trunc", Date.class , cb.literal("day"), root.get("startTime"));

List<Object[]> result = entityManager.createQuery(query.select(exp).groupBy(exp)).getResultList();

但我得到了异常(exception):

Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2122)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1905)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1881)
    at org.hibernate.loader.Loader.doQuery(Loader.java:925)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
    at org.hibernate.loader.Loader.doList(Loader.java:2622)
    at org.hibernate.loader.Loader.doList(Loader.java:2605)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2434)
    at org.hibernate.loader.Loader.list(Loader.java:2429)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1339)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:483)
    ... 122 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: column "examplei0_.start_time" must appear in the GROUP BY clause or be used in an aggregate function
  Position: 69

我做错了什么?

休眠 SQL:

select date_trunc(?, examplei0_.start_time) as col_1_0_ from examplei0_ group by date_trunc(?, examplei0_.start_time)

如果我在数据库控制台中执行它,它会起作用。

最佳答案

我猜 Postgres 看到了

select date_trunc(?, examplei0_.start_time) 作为 col_1_0_ from examplei0_ group by date_trunc(?, examplei0_.start_time)

并拒绝,因为第一个 date_trunc(?, examplei0_.start_time) 不一定与第二个 date_trunc(?, examplei0_.start_time) 相同传入的实际参数。

如果是这种情况,您需要休眠来生成一个查询,其中 'day' 没有参数化。或者,在 postgres date_trunc_day(timestamp) 中创建一个调用 date_trunc('day', timestamp) 的函数并改为调用新函数。

关于postgresql - 在 JPA 中按截断日期分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48994080/

相关文章:

postgresql - 如何在 postgresql 中返回 0 而不是 NULL?

java - Hibernate JPA 删除分离对象时没有异常

sql - hibernate -HQL连接许多子句

java - 如何在 Hibernate 中向时间戳列添加分钟?

java - 如何从 JPA 中的父实体记录中删除子实体?

variables - 带有变量的Grails FindAllWhere

postgresql - 为什么即使在调用 nextval 之后 pgsql 仍声称序列的 currval 未定义?

postgresql - Postgres - 加入更新给出了错误的结果

sql - 在 POSTGRESQL 中按名称访问列

java - 远程处理案例中的惰性/急切加载策略 (JPA)