0

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)
1
  • I think you should use java.sql.Date. Commented Jan 14, 2014 at 9:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It is not a solution, when timestamp is used in DB than hibernate will throw exception 'Found: timestamp, expected: date'.
The annotation will not fix your existing DDL, how is it supposed to do that? But during DDL generation from JPA entities, it will tell the JPA provider to create a DATE column, which was OPs requirement.
@whatswrong - IMHO you have misunderstood OP's requirement and the downvote is unwarranted. Your issue is a different one, you can post a separate question for it. If you think I have misunderstood OP's, please explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.