user1023622 user1023622 - 1 year ago 142
Java Question

How can I map a Java date to DATETIME in mysql (by default its TIMESTAMP) with Hibernate annotations

All my db tables should have an endTime field which by default should be END_OF_TIME or something like that. I am not happy about the 2038 limitation so I want endTime to be of type DATETIME in mysql.

My Java code is:

public class BaseDBEntity {
public String id;

public Date startTime;

public Date endTime;

public BaseDBEntity() {


I can work around by creating the table manually with an endTime field of type DATETIME, and than map the entity endTime to that column, however I would like Hibernate to generate the tables automatically - how can I do that?

Answer Source

Use the columnDefinition attribute of the @Column annotation:

@Column(name = "startTime", columnDefinition="DATETIME")
private Date startTime;

And please, make your attributes private.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download