简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM