Spring Boot将无效的datetime插入到MariaDB中,结果为null,在不可为空的字段

时间:2017-08-15 12:54:40

标签: hibernate spring-boot kotlin mariadb

我在Spring Boot项目中遇到问题,我有一个像这样创建的表:

create table task
(
    id int auto_increment
        primary key,
    title varchar(50) not null,
    content text not null,
    control_time datetime not null,
);

和这样的实体:

@Entity
data class Task(
        @Id @GeneratedValue var id: Long = 0,
        @Column(length = 50) var title: String = "",
        @Column(columnDefinition = "TEXT", length = 65535) var content = "",
        @Temporal(TemporalType.TIMESTAMP) var controlTime: Date = Date())

我还有一个端点,它将一个Task作为参数,Jackson将其从JSON转换为Task对象,可以很好地查看调试日志。

所以问题在于,如果我给出一个超出合法范围的日期(例如,在9999年之上),那么hibernate会保持它没有错误,但是该字段最终包含null,这在它的时候是不可能的。一个不可空的领域。我会假设mariadb会抛出一个错误,如果你坚持超出合法范围的东西,当我手动操作时会这样做,但不知何故,hibernate设法做到这一点,并且它最终为空。

来自休眠的跟踪:

26543 --- [nio-8080-exec-1] o.h.p.entity.AbstractEntityPersister     : Dehydrating entity: [something.model.Task#<null>]
2017-08-15 14:28:22.220 TRACE 26543 --- [nio-8080-exec-1]     o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [Tue Aug 15 14:28:22 CEST 2017]
2017-08-15 14:28:22.221 TRACE 26543 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [NIKlas]
2017-08-15 14:28:22.221 TRACE 26543 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [TIMESTAMP] - [null]
2017-08-15 14:28:22.222 TRACE 26543 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [TIMESTAMP] - [null]
2017-08-15 14:28:22.222 TRACE 26543 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [VARCHAR] - [null]
2017-08-15 14:28:22.222 TRACE 26543 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [TIMESTAMP] - [null]
2017-08-15 14:28:22.222 TRACE 26543 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [7] as [BIGINT] - [null]
2017-08-15 14:28:22.222 TRACE 26543 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [8] as [VARCHAR] - []
2017-08-15 14:28:22.223 TRACE 26543 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [9] as [TIMESTAMP] - [Mon Sep 10 05:06:44 CEST 12439173]
2017-08-15 14:28:22.224 TRACE 26543 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [10] as [BOOLEAN] - [null]
2017-08-15 14:28:22.224 TRACE 26543 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [11] as [VARCHAR] - []
2017-08-15 14:28:22.228 DEBUG 26543 --- [nio-8080-exec-1] o.h.id.IdentifierGeneratorHelper         : Natively generated identity: 2
2017-08-15 14:28:22.228 TRACE 26543 --- [nio-8080-exec-1] o.h.r.j.i.ResourceRegistryStandardImpl   : Releasing result set [org.mariadb.jdbc.internal.queryresults.resultset.MariaSelectResultSet$1@1c4fe495]
2017-08-15 14:28:22.228 DEBUG 26543 --- [nio-8080-exec-1] o.h.r.j.i.ResourceRegistryStandardImpl   : HHH000387: ResultSet's statement was not registered
2017-08-15 14:28:22.228 TRACE 26543 --- [nio-8080-exec-1] o.h.r.j.i.ResourceRegistryStandardImpl   : Closing result set [org.mariadb.jdbc.internal.queryresults.resultset.MariaSelectResultSet$1@1c4fe495]
2017-08-15 14:28:22.228 TRACE 26543 --- [nio-8080-exec-1] o.h.r.j.i.ResourceRegistryStandardImpl   : Releasing statement [HikariProxyPreparedStatement@639958422 wrapping sql : 'insert into task (created_at, created_by, deleted_at, updated_at, updated_by, closed_at, closed_by, content, control_time, is_successful, title) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', parameters : ['2017-08-15 14:28:22.147','NIKlas',<null>,<null>,<null>,<null>,<null>,'','12439173-09-10 05:06:44.928',<null>,'']]
2017-08-15 14:28:22.229 TRACE 26543 --- [nio-8080-exec-1] o.h.r.j.i.ResourceRegistryStandardImpl   : Closing prepared statement [HikariProxyPreparedStatement@639958422 wrapping sql : 'insert into task (created_at, created_by, deleted_at, updated_at, updated_by, closed_at, closed_by, content, control_time, is_successful, title) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', parameters : ['2017-08-15 14:28:22.147','NIKlas',<null>,<null>,<null>,<null>,<null>,'','12439173-09-10 05:06:44.928',<null>,'']]
2017-08-15 14:28:22.229 TRACE 26543 --- [nio-8080-exec-1] o.h.e.jdbc.internal.JdbcCoordinatorImpl  : Starting afterQuery statement execution processing [ON_CLOSE]
2017-08-15 14:28:22.231 DEBUG 26543 --- [nio-8080-exec-1] o.h.e.t.internal.TransactionImpl         : committing
2017-08-15 14:28:22.231 TRACE 26543 --- [nio-8080-exec-1] cResourceLocalTransactionCoordinatorImpl : ResourceLocalTransactionCoordinatorImpl#beforeCompletionCallback
2017-08-15 14:28:22.231 TRACE 26543 --- [nio-8080-exec-1] org.hibernate.internal.SessionImpl       : SessionImpl#beforeTransactionCompletion()
2017-08-15 14:28:22.232 TRACE 26543 --- [nio-8080-exec-1] org.hibernate.internal.SessionImpl       : Automatically flushing session
2017-08-15 14:28:22.232 TRACE 26543 --- [nio-8080-exec-1] o.h.e.i.AbstractFlushingEventListener    : Flushing session

我不知道如何加粗相关部分,但正如你所看到的那样插入日期时间“12439173-09-10 05:06:44.928”而不抱怨。 然后,当我查看数据库时,“control_time”的值为null。

有没有人遇到过这个问题,或者知道怎么会发生这种情况?

编辑:定义中的实体与休眠跟踪之间存在一些不匹配,因为定义已经简化。

0 个答案:

没有答案