Bick
Bick

Reputation: 18511

java hibernate - how do i map a date column as datetime?

In my sql there are the following date types: Date ,Time,Year, Datetime, timestamp.

my class holds a date object. How can I choose which type should be created in the DB?

Upvotes: 4

Views: 8148

Answers (1)

esaj
esaj

Reputation: 16035

You can mark the Date-object with @Temporal-annotation:

In plain Java APIs, the temporal precision of time is not defined. When dealing with temporal data you might want to describe the expected precision in database. Temporal data can have DATE, TIME, or TIMESTAMP precision (ie the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.

@Temporal(TemporalType.TIMESTAMP) 
private Date someDate;

Upvotes: 7

Related Questions