Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA标准API在复杂的GROUP BY上失败_Java_Postgresql_Hibernate_Jpa_Criteria Api - Fatal编程技术网

Java JPA标准API在复杂的GROUP BY上失败

Java JPA标准API在复杂的GROUP BY上失败,java,postgresql,hibernate,jpa,criteria-api,Java,Postgresql,Hibernate,Jpa,Criteria Api,给定的表只包含主键和DATETIME字段“create_date”,我正在尝试使用JPA criteria api编写查询,该api将统计从给定日期开始的不同时间间隔(例如2小时、12小时、3天)中创建的行数(下面代码中的agrumentintervalStart)。在未来,将添加额外的列,用户应该能够根据这些列筛选结果-因此需要动态构建查询。查询应该在PostgreSQL 9上工作+ 实体类: @Entity(name = "foo") public class Foo { @Id

给定的表只包含主键和DATETIME字段“create_date”,我正在尝试使用JPA criteria api编写查询,该api将统计从给定日期开始的不同时间间隔(例如2小时、12小时、3天)中创建的行数(下面代码中的agrument
intervalStart
)。在未来,将添加额外的列,用户应该能够根据这些列筛选结果-因此需要动态构建查询。查询应该在PostgreSQL 9上工作+

实体类:

@Entity(name = "foo")
public class Foo {

    @Id
    @Column(name = "id", nullable = false)
    private long id;

    @Column(name = "create_date", columnDefinition = "TIMESTAMP")
    private LocalDateTime createDate;
}
生成查询的代码(12小时间隔):

但是,如果我将创建的SQL复制到数据库控制台,它将成功执行并生成预期结果

19:15:24.369 DEBUG [main] o.h.SQL - select floor((1497106800-date_part('epoch', foo0_.create_date))/7200) as col_0_0_, count(foo0_.id) as col_2_0_ from foo foo0_ group by floor((1497106800-date_part('epoch', foo0_.create_date))/7200) order by floor((1497106800-date_part(?, foo0_.create_date))/7200) asc
19:15:24.370 TRACE [main] o.h.t.d.s.BasicBinder - binding parameter [1] as [VARCHAR] - [epoch]
19:15:24.391 WARN  [main] o.h.e.j.s.SqlExceptionHelper - SQL Error: 0, SQLState: 42803
19:15:24.392 ERROR [main] o.h.e.j.s.SqlExceptionHelper - ERROR: column "foo0_.create_date" must appear in the GROUP BY clause or be used in an aggregate function
  Position: 407

我怀疑错误信息只是有点误导,而
地板中的
部分((1497106800-date\u部分(?,foo0.create\u date))/7200)
才是真正的罪魁祸首。Postgres引擎不能自动假定它实际上是隐藏在参数值内的
'epoch'
,因此报告
选择
按顺序
之间不匹配
条款对于
日期部分(“epoch”),可以采取解决办法,防止Hibernate将
'epoch'
转换为参数。我从来没有试过,但是,我不能保证它会work@crizzis谢谢你的建议,看来你是对的。当我删除查询的“orderby”部分时,它最终开始工作并返回结果(在PostgreSQL上)。以前,我在h2数据库上尝试过类似的查询,但在那里我得到了相同的异常事件,没有“orderby”。
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1535)
    at org.hibernate.query.Query.getResultList(Query.java:165)
    at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:76)
    (...)
    Caused by: org.postgresql.util.PSQLException: ERROR: column "foo0_.create_date" must appear in the GROUP BY clause or be used in an aggregate function
  Position: 407
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2533)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2268)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:313)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369)
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:159)
    at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:109)
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:57)
    ... 135 more
19:15:24.369 DEBUG [main] o.h.SQL - select floor((1497106800-date_part('epoch', foo0_.create_date))/7200) as col_0_0_, count(foo0_.id) as col_2_0_ from foo foo0_ group by floor((1497106800-date_part('epoch', foo0_.create_date))/7200) order by floor((1497106800-date_part(?, foo0_.create_date))/7200) asc
19:15:24.370 TRACE [main] o.h.t.d.s.BasicBinder - binding parameter [1] as [VARCHAR] - [epoch]
19:15:24.391 WARN  [main] o.h.e.j.s.SqlExceptionHelper - SQL Error: 0, SQLState: 42803
19:15:24.392 ERROR [main] o.h.e.j.s.SqlExceptionHelper - ERROR: column "foo0_.create_date" must appear in the GROUP BY clause or be used in an aggregate function
  Position: 407