I have a problem in a Spring Boot project, where I have a table created like this:
create table task
(
id int auto_increment
primary key,
title varchar(50) not null,
content text not null,
control_time datetime not null,
);
and the entity like this:
@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())
I also have an endpoint which takes a Task as parameter, where Jackson converts it from JSON to a Task Object, which works fine looking at debug logs.
So the problem is that if I give a date which is outside the legal range(eg. above the year 9999), hibernate persists it without error, but the field ends up containing null, which shouldn't be possible when it's a not nullable field. I would assume mariadb throws an error if you persist something outside the legal range, which it does when i do it manually, but somehow hibernate manages to do it, and it ends up being null.
Trace from 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
I don't know how to bold the relevant parts, but as you can see it inserts the datetime "12439173-09-10 05:06:44.928" without complaining. When I then look in the database the value of "control_time" is null.
Has anyone ever encountered this problem, or know how this could happen?
EDIT: there are some mismatches between entity in definition and hibernate trace, because the definition is simplified.