无法转换价值' 2017-01-26 11:47:54.000000'从第14栏到TIMESTAMP

时间:2017-01-26 07:23:46

标签: java mysql sql-server spring hibernate

注意:这不是重复问题。

我按照this教程将MSSQL数据库迁移到了Mysql数据库。

在表格date_in中有一个日期时间提交说main。因此,当我使用mysql查询选择date_in时,它可以正常工作,如下所示

SELECT DATE_IN FROM incoming_audit.main where DATE_IN is not null limit 0, 50000;

enter image description here

在Java中 - Spring-Hibernate当我尝试从main表中获取前25条记录时,它会给我以下错误

错误(不允许我粘贴无效字符,请参见下图)

Hibernate: select this_.ID as ID1_1_0_, this_.AVAILABLE as AVAILABL2_1_0_, this_.BATTERY as BATTERY3_1_0_, this_.BOX as BOX4_1_0_, this_.CAPS as CAPS5_1_0_, this_.COA_EDITION as COA_EDIT6_1_0_, this_.COA_VERSION as COA_VERS7_1_0_, this_.CPU as CPU8_1_0_, this_.CPU_CORES as CPU_CORE9_1_0_, this_.CPU_DATA_WIDTH as CPU_DAT10_1_0_, this_.CPUSPD as CPUSPD11_1_0_, this_.CUSTOMER_ASSET as CUSTOME12_1_0_, this_.CUSTOMER_ID as CUSTOME13_1_0_, this_.DATE_IN as DATE_IN14_1_0_, this_.DATE_INC as DATE_IN15_1_0_, this_.FORM_FACTOR as FORM_FA16_1_0_, this_.HDD_INC_SERIAL as HDD_INC17_1_0_, this_.HDD_MODEL as HDD_MOD18_1_0_, this_.HDD_SERIAL as HDD_SER19_1_0_, this_.HDD_SIZE as HDD_SIZ20_1_0_, this_.HDD_SMART as HDD_SMA21_1_0_, this_.INC_TECH as INC_TEC22_1_0_, this_.INT_SERIAL as INT_SER23_1_0_, this_.KILLDISK as KILLDIS24_1_0_, this_.LOCATION as LOCATIO25_1_0_, this_.LOT_ID as LOT_ID26_1_0_, this_.MANUFACTURER as MANUFAC27_1_0_, this_.MODEL as MODEL28_1_0_, this_.NOTES as NOTES29_1_0_, this_.PALETTE as PALETTE30_1_0_, this_.PLASTIC_CONDITION as PLASTIC31_1_0_, this_.POWER_ADAPTER as POWER_A32_1_0_, this_.PXE_TECH as PXE_TEC33_1_0_, this_.RAM_PER_SLOT as RAM_PER34_1_0_, this_.RAM_SLOTS as RAM_SLO35_1_0_, this_.SCREEN_CONDITION as SCREEN_36_1_0_, this_.SERIAL as SERIAL37_1_0_, this_.TOTAL_RAM as TOTAL_R38_1_0_, this_.WEBCAM as WEBCAM39_1_0_ from Main this_ limit ?
2017-01-26 11:48:33 [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] WARN   - SQL Error: 0, SQLState: S1009
2017-01-26 11:48:33 [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] ERROR  - Cannot convert value '2017-01-26 11:47:54.000000' from column 14 to TIMESTAMP.
Jan 26, 2017 11:48:33 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/sts] threw exception [Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query] with root cause
java.lang.NumberFormatException: 81646
SOLDRETAILû

enter image description here

列映射

@Type(type = "timestamp")
@Column(name = "DATE_IN", nullable = true)
private Date dateIn;

如何删除无效字符?

更新

当我在查询下面运行时,我的代码运行完美

UPDATE incoming_audit.main SET DATE_IN=null WHERE DATE_IN is not null;

但是当我在运行上面的查询后运行以下查询时,再次出现无效字符异常

UPDATE incoming_audit.main SET DATE_IN=now() WHERE DATE_IN is null;

但这一次是不同的例外。

enter image description here

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,对于正在寻找选项的人,

Caused by: java.sql.SQLException: Cannot convert value '2021-04-23 10:43:54.000000' from column 15 to TIMESTAMP.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) ~[mysql-connector-java-5.1.7.jar:?]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) ~[mysql-connector-java-5.1.7.jar:?]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) ~[mysql-connector-java-5.1.7.jar:?]
    at com.mysql.jdbc.ResultSetRow.getTimestampFast(ResultSetRow.java:1328) ~[mysql-connector-java-5.1.7.jar:?]
    at com.mysql.jdbc.BufferRow.getTimestampFast(BufferRow.java:573) ~[mysql-connector-java-5.1.7.jar:?]

此列的数据类型为 datetime(6)。数据是通过休眠添加的,当我尝试加载数据时出现此错误。

该列与上次修改时间戳相关。

@Column(
        name = "last_modified_time",
        nullable = false
)
@Temporal(TemporalType.TIMESTAMP)
private Date lastModifiedTime;

因为 timestamp 更适合这里,所以我使用了 timestamp 数据类型而不是 datetime(6),这样无需任何其他修改即可解决问题。