【发布时间】:2017-12-31 04:24:35
【问题描述】:
我无法使用查询中的日期从数据库中获取数据...
我正在开发使用 Spring Data JPA 和 Oracle 数据库的 Web 应用程序。我在接口中使用了 @RepositoryRestResource 注释,我只是使用 @Param 声明了一些带有命名参数的查询方法和 @Query 注释。今天我需要添加一个带有日期的新实体。在数据库中,两列都是 DATE 类型,并且在查询中使用。但我也有另一种类型的 TIMESTAMP ,也许我将来需要使用。在这两个列的 Java 表示下方,当然还有所有 setter 和 getter,但它有更多字段,所以只需添加以下内容:
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "INIT_DATE")
private Calendar initDate;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "AGG_DATE")
private Calendar aggDate;
我还为案例创建了新界面,与往常一样:
@RepositoryRestResource(collectionResourceRel = "customer", path = "customer")
public interface ICustomerRepository extends PagingAndSortingRepository<Customer, Long> {
@Query("SELECT c FROM Customer c where c.initDate <= TO_DATE(:currentDate, 'yyyy-MM-dd') AND c.aggDate >= TO_DATE(:currentDate, 'yyyy-MM-dd')")
public List<Customer> filterByDate(@Param("currentDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Calendar currentDate);
}
我收到了这个错误:
org.springframework.dao.DataIntegrityViolationException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not extract ResultSet
ORA-01858: a non-numeric character was found where a numeric was expected
我正在尝试使用此 http 请求从数据库中获取此数据:
http://localhost/webApp/customer/search/filterByDate?currentDate=2017-07-10
在 SQL Developer 中,查询工作正常。
我在某处读到 JPQL 中没有日期函数,但在日志中我可以看到如下所示的查询和参数:
select
customer0_.customerId as col_0_0_,
customer0_.customerName as col_0_1_,
customer0_.aggDate as col_0_2_,
customer0_.initDate as col_0_3_,
from
customer customer0_
where
and customer0_.aggDate>=to_date(?, 'yyyy-MM-dd')
and customer0_.initDate<=to_date(?, 'yyyy-MM-dd')
2017-07-25 11:55:22.550 TRACE 12252 --- [ (self-tuning)'] o.h.type.descriptor.sql.BasicBinder : binding parameter [8] as [TIMESTAMP] - [java.util.GregorianCalendar[time=1499637600000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Berlin",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Europe/Berlin,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=6,WEEK_OF_YEAR=28,WEEK_OF_MONTH=3,DAY_OF_MONTH=10,DAY_OF_YEAR=191,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=3600000,DST_OFFSET=3600000]]
2017-07-25 11:55:22.550 TRACE 12252 --- [ (self-tuning)'] o.h.type.descriptor.sql.BasicBinder : binding parameter [9] as [TIMESTAMP] - [java.util.GregorianCalendar[time=1499637600000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Berlin",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Europe/Berlin,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=6,WEEK_OF_YEAR=28,WEEK_OF_MONTH=3,DAY_OF_MONTH=10,DAY_OF_YEAR=191,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=3600000,DST_OFFSET=3600000]]
老实说,我不知道这里有什么问题...数据库中的格式日期是 yy/MM/DD,但它也不适用于我...你能告诉我什么我错过了还是做错了??
编辑 [Gusti Arya 的回答]:
我尝试了两件事。在您更新之前,我只是更改了类型并离开了@DateTimeFormat。然后我遇到了与日历类型相同的错误。 删除@DateTimeFormat 后,更新后也一样,我收到此错误:
org.springframework.data.repository.support.QueryMethodParameterConversionException: Failed to convert 2017-07-10 into java.util.Date!
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [org.springframework.data.repository.query.Param java.util.Date] for value '2017-07-10'; nested exception is java.lang.IllegalArgumentException
更有趣的是,我有几乎相同的第二个查询,但没有 TO_DATE 函数,现在我遇到了与上述相同的错误。以前我有:
Persistent Entity Must not be null
EDIT 2 [与日志中的查询有关]:
我刚刚注意到我在此处发布的查询与我在日志中看到的不同...我的实体包含 EmbeddedId,其中是 customerId 和 customerName。这两列出现三次......所以这是查询的有效日志:
select
customer0_.customerId as col_0_0_,
customer0_.customerName as col_0_1_,
customer0_.customerId as col_1_0_,
customer0_.customerName as col_1_1_,
customer0_.customerId as customerId1_6_,
customer0_.customerName as customerName2_6_,
customer0_.aggDate as aggDate3_6_,
customer0_.initDate as initDate4_6_,
from
customer customer0_
where
and customer0_.aggDate>=to_date(?, 'yyyy-MM-dd')
and customer0_.initDate<=to_date(?, 'yyyy-MM-dd')
**编辑[对布赖恩的回应]:**
Entity 中的类型也应该是 Calendar,对吗?我还为这两个字段添加了两个不同的 Temporal 注释。一个指向 TemporalType.TIMESTAMP,另一个指向 TemporalType.DATE。但是,将日历值作为 http 参数传递的问题。我尝试了四个版本的 URL:
1. http://localhost/webApp/customer/search/filterByDate?currentDate=2017-07-10
2. http://localhost/webApp/customer/search/filterByDate?currentDate=2017-07-10 13:08:24.000+0000
3. http://localhost/webApp/customer/search/filterByDate?currentDate=2017-07-10T13:08:24.000+0000
4. http://localhost/webApp/customer/search/filterByDate?currentDate=1499692104
但是这些都行不通……
【问题讨论】:
-
不确定 spring,但您可以尝试
hibernate @type在时间戳上使用 date 类型注释 -
它没有帮助。同样的错误。
-
你能把整个
stacktrace发帖吗? -
使用
oracle's timestamp -
它可以与 (stackoverflow.com/questions/2188768/…) 重复
标签: java rest spring-boot spring-data-jpa jpql