java - 如何使用 JPA 和 DB2 将 DATETIME 列映射到 Date 对象

标签 java date jpa db2 spring-data

我正在尝试查询 DB2 数据库并使用 JavaDATEIME 列映射到 Date,但出现此错误:

DB2 SQL 错误:SQLCODE=-181,SQLSTATE=22007,SQLERRMC=null,DRIVER=4.21.29

Short Description: THE STRING REPRESENTATION OF A DATETIME VALUE IS NOT A VALID DATETIME VALUE C

The string representation of a datetime is not in the acceptable range or is not in the correct format. The proper ranges for datetime values are as follows: Table 2. Range of datetime values Datetime Numeric Range Years 0001 to 9999 Months 1 to 12 Days April, June, September, November (months 4, 6, 9, 11) 1 to 30 February (month 2) 1 to 28 (Leap year 1 to 29) January, March, May, July, August, October, December (months 1, 3, 5, 7, 8, 10, 12) 1 to 31 Hours 0 to 24 (If hour is 24, other parts of time values are zeroes. If hour is USA, maximum hour is 12.) Minutes 0 to 59 Seconds 0 to 59 Microseconds 0 to 999999 System action: The statement cannot be executed. Programmer response: Check whether the value is within the valid range and is in the proper format. Refer to Chapter 2 of DB2 SQL Reference for information on string data formats.

首先我将 LocalDate 转换为 Date

public static Date convertLocalDateToDate(LocalDate date) {
    return  Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
}

其次,我正在使用规范 API

public static Specification<MyEntity> dataUse(Date dataUse){
    return (root, query, builder) -> builder.equal(root.get("myDate"), dataUse);
}

我在实体中的字段映射为

@Column(name="DT_EX_MOVI_IN")
@Temporal(TemporalType.DATE)
private Date myDate;

最后当我尝试执行查询时

repository.findAll(query);

我收到错误 DB2 SQL Error: SQLCODE=-181, SQLSTATE=22007, SQLERRMC=null, DRIVER=4.21.29

有人可以帮助我吗?

谢谢

最佳答案

我解决了我的问题。我阅读了 DB2 的文档 https://www.ibm.com/support/knowledgecenter/en/SSEPEK_10.0.0/java/src/tpc/imjcc_rjvjdata.html

我的解决方案是用 java.sql.Date 映射我的对象。

非常感谢所有试图帮助我的人!

关于java - 如何使用 JPA 和 DB2 将 DATETIME 列映射到 Date 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57101627/

相关文章:

java - 通过 REST API : allocatedMB, allocatedVcores 和 runningContainers 的 Hadoop 指标始终为 -1

PHP按日期排序多维数组

java - 每个功能都有EntityManagerFactory?

java - JPA ORM关系高效吗?

java - JPA 与 Google Cloud SQL "NoClassDefFoundError: java.util.prefs.Preferences is a restricted class"

java - 如何在javafx中的PrinterJob中设置目标打印机

java - 为什么Java中没有(123 == 0123)?

Java NIO Sockets 客户端请求处理

java - Joda-Time——从另一个句点中删除一个句点

php - 当本地日期格式不同时处理 mySQL 日期格式