I need to access an existing MySQL database which uses BIGINT columns to store timestamps:
create table mytable (created bigint);
Now I prefer to work with java.util.Date or java.time.Instant instances instead of integers, so I’m trying to let Hibernate convert the values directly. Unfortunately Hibernate won’t recognize the column when annotated like this:
@Column(name = "created")
private Date created;
or like this:
@Column(name = "created")
@Temporal(TemporalType.TIMESTAMP)
private Calendar created;
This does return something in the future like 2017-07-01T04:14:00+02:00. How can I make Hibernate convert BIGINT columns properly so I don’t have to convert them in getters and setters?
Hibernate’s
TemporalType.TIMESTAMPmaps tojava.sql.Timestamp. TheTimestampconstructor takes Long values which is the number of milliseconds since the epoch. The values in database are stored as Unix timestamps which is the number of seconds since the epoch.I wrote a
UnixTimestampTypethat takes seconds instead of milliseconds and createsDateinstances:and