Why does Hibernate ORM uses DATETIME by default on MySQL instead of TIMESTAMP


#1

Greetings!

I am currently developing an application and I am learning Hibernate ORM as I go along. I am using version 5.2.14 through Spring Boot. My database is MySQL 5.7.20.

I then used the hbm2ddl feature to create my database schema from my entity classes, however I was confused when Hibernate chose to generate a DATETIME column for my java.time.Instant property. I then experimented a bit (using java.util.Date with TemporalType.TIMESTAMP), but no matter what I tried, Hibernate insists on using DATETIME. I asked this question on stackoverflow, which did not provide much value, so I went digging deeper. Now I have found that this seems to be intentional, since the MySQLDialect class registers DATETIME for use with the TIMESTAMP type.

My question now is, why is this? I know I can make Hibernate “bend to my will”, by either not creating my Schema automatically or using the columnDefinition property in my annotations, however I feel there is a reason for not using TIMESTAMP in MySQL. Is there anyone who can enlighten me as to what it is?

Thanks!

Edit: I have found this old issue report, which was closed with a simple “DATETIME is correct”. :frowning:


#2

TIMESTAMP has a very narrow range: 1970-2038, making it less practical than DATETIME.

More, TIMESTAMP also has automatic initialization for INSERT and UPDATE which does not work well with @DynamicInsert or @DynamicUpdate.

For these reasons, DATETIME makes a much better candidate for Hibernate than TIMESTAMP.

However, you are free to either override the MySQLDialect or just change the columnDefinition to TIMESTAMP.


#3

What! Okay, thank you, I was not aware of that sillyness on MySQLs part. DATETIME is indeed the “better option” then, even though not ideal (because you have to make sure to always convert to UTC before storing).
But that explains it. Another thing to add to the list of MySQL quirks to randomly bash on.

Closed!