Spring数据@Query joda datetime postgresql异常

时间:2018-06-05 17:29:05

标签: postgresql spring-data spring-data-jpa

我有一个带有joda DateTime的2列实体。

@Column(name = "track_start", columnDefinition= "TIMESTAMP WITH TIME ZONE")
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime start;

@Column(name = "tracke_end", columnDefinition= "TIMESTAMP WITH TIME ZONE")
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime end;

然后我有一个带有find方法的Spring数据存储库:

@Query("SELECT tt FROM Timetrack tt "
        + "WHERE (:dateFrom is null OR tt.start >= :dateFrom) "
        + "AND (:dateTo is null OR tt.end <= :dateTo)")
Page<Timetrack> findBy(@Param("dateFrom") DateTime dateFrom,
                       @Param("dateTo") DateTime dateTo,
                       Pageable pageable);

如果我调用该finder方法,我会得到一个异常:

org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $2
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455)

如果我将@Param更改为Date而不是DateTime,我得到了这个例外:

java.lang.IllegalArgumentException: Parameter value [Fri Jun 01 19:26:44 CEST 2018] did not match expected type [org.joda.time.DateTime (n/a)]
at org.hibernate.jpa.spi.BaseQueryImpl.validateBinding(BaseQueryImpl.java:897)
at org.hibernate.jpa.internal.QueryImpl.access$000(QueryImpl.java:61)

有人有想法吗?

这里是hibernate的日志输出:

Hibernate: select timetrack0_.id as id1_6_, timetrack0_.created as created2_6_, timetrack0_.entity_status as entity_s3_6_, timetrack0_.updated as updated4_6_, timetrack0_.user_identity_id as user_ide9_6_, timetrack0_.tracke_end as tracke_e5_6_, timetrack0_.frozen_at as frozen_a6_6_, timetrack0_.invoice_id as invoice10_6_, timetrack0_.project_id as project11_6_, timetrack0_.track_start as track_st7_6_, timetrack0_.task as task8_6_ from timetrack timetrack0_ where ( timetrack0_.entity_status = 'active') and (? is null or timetrack0_.track_start>=?) and (? is null or timetrack0_.tracke_end<=?) order by timetrack0_.track_start asc limit ? offset ?
2018-06-09 21:32:05.804 TRACE 14006 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [2018-06-09 20:32:05.604]
2018-06-09 21:32:05.806 TRACE 14006 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [TIMESTAMP] - [2018-06-09 20:32:05.604]
2018-06-09 21:32:05.808 TRACE 14006 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [TIMESTAMP] - [2018-06-10 02:32:05.604]
2018-06-09 21:32:05.809 TRACE 14006 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [TIMESTAMP] - [2018-06-10 02:32:05.604]

0 个答案:

没有答案