I'm using Hibernate 5.2.8. I have an entity with a timestamp:
Quote:
@NotNull
@Column(nullable = false)
private Timestamp created;
The database definitions for it are...
in postgres: "timestamp without time zone"
in mysql: "datetime"
To my knowledge, these properties should all line up fine. But the discrepancy occurs if I do something like this before persisting the entity:
Code:
created = new Timestamp(0L)
Here's the discrepancy: what I would expect from the above is essentially a timestamp at epoch time, i.e. zero. In postgres I get this result. However, in mysql, the result is apparently a timestamp of the current time, rather than epoch time. This is apparent if I select unix_timestamp(created) .
Seems like there is (and has been since the 2000s) a lot of discussion online regarding confusion in how timestamps are stored by hibernate for mysql vs. others, but unfortunately a lot of it is the blind leading the blind, with a whole lot of misunderstanding about the difference between Java's Timestamp and Date objects. As such I haven't found a fix yet. People are claiming that a DateTime or Date object should be used in the entity instead of Timestamp, which seems wrong and required a @Temporal annotation anyway.
Any ideas? Or am I doing the entity field wrong?
Although this bug from 2005 has to do with generating SQL from entities, it sounds kinda similar:
https://hibernate.atlassian.net/browse/HB-265