[英]Using joda DateTime in parameters for native query in spring-data-jpa fails because of bad binding
Here are two queries that should be exactly the same except one is marked as native and the other is not. 这里有两个查询应该完全相同,除了一个标记为本机而另一个不标记为本机。 The first one works fine, but the second one fails with
incompatible data type in conversion
第一个工作正常,但第二个
incompatible data type in conversion
失败, incompatible data type in conversion
@Transactional(readOnly = true)
@Query(value = "select startDate from TaskMetrics where startDate between :startDate and :endDate")
List<DateTime> findStartDateByStartDateBetween(@Param("startDate") DateTime startDate,
@Param("endDate") DateTime endDate);
This generates the query: 这会生成查询:
select taskmetric0_.startDate as col_0_0_ from TaskMetrics taskmetric0_ where taskmetric0_.startDate between ? and ?
With bindings 有了绑定
binding parameter [1] as [TIMESTAMP] - [2015-02-02 10:57:14.279]
binding parameter [2] as [TIMESTAMP] - [2015-02-04 10:57:14.281]
- -
@Transactional(readOnly = true)
@Query(nativeQuery = true, value = "select startDate from TaskMetrics where startDate between :startDate and :endDate")
List<DateTime> findStartDateBetween(@Param("startDate") DateTime startDate,
@Param("endDate") DateTime endDate);
This generates the query: 这会生成查询:
select startDate from TaskMetrics where startDate between ? and ?
With one binding, which also seems a bit odd (especially why #2?): 有一个绑定,这似乎有点奇怪(特别是为什么#2?):
binding parameter [2] as [VARBINARY] - [2015-02-04T10:57:14.315-05:00]
I'm using Hibernate 4.3.8.Final as my JPA 2.1 provider and Jadira Usertype 3.1.0.CR10 for JodaTime support. 我使用Hibernate 4.3.8.Final作为我的JPA 2.1提供程序,使用Jadira Usertype 3.1.0.CR10作为JodaTime支持。
Am I doing something wrong or is this a bug somewhere? 我做错了什么,或者这是某个地方的错误?
Bug opened here - https://jira.spring.io/browse/DATAJPA-671 Bug在这里打开 - https://jira.spring.io/browse/DATAJPA-671
Workaround for the bug: 该bug的解决方法:
public class MyDaoClass {
// Converter provided by joda time user type library
private TimestampColumnDateTimeMapper columnDateTimeMapper = new TimestampColumnDateTimeMapper();
@Override
public void doAction(MyObject myObject) {
Query q = getEntityManager().createNamedQuery("MyNativeNamedQuery");
q.setParameter("date", columnDateTimeMapper.toNonNullValue(myObject.getDate()), TemporalType.TIMESTAMP);
q.executeUpdate(); // or select
}
The second one is a nativeQuery
as i know the will not work with joda DateTime. 第二个是
nativeQuery
因为我知道它不适用于joda DateTime。 use Java.sql.Date
instead. 请改用
Java.sql.Date
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.