membersound
membersound

Reputation: 86925

How to tell hibernate to create Date instead of timestamp in DB?

How can I tell hibernate that a Date should be created as a Date in postgres SQL database, and not as a timestamp? According to www.postgresql.org/docs/9.3/static/datatype-datetime.html Date should be possible, but how?

@Entity
class Test {
    private java.util.Date date;
}

becomes:

CREATE TABLE test
(date timestamp without time zone)

Upvotes: 0

Views: 2027

Answers (1)

kostja
kostja

Reputation: 61578

Use the Temporal annotation:

@Temporal(TemporalType.DATE)
private Date date;

There are also TIME and TIMESTAMP TemporalTypes. TIMESTAMP is the default.

EDIT. You can also use one of the appropriate SQL types: java.sql.Date, java.sql.Time or java.sql.Timestamp.

The DB mapping is identical, whether you use java.util.Date with annotations or java.sql.* Using java.util.Date gives you a consistent interface regardless of the physical DB mapping while sql types may make your intent clearer.

Upvotes: 1

Related Questions