Reputation: 7084
I have field in Entity:
@Column(name = "BILL_DATE")
private LocalDate billDate;
And my system work with oracle
and posthresql
. In posgresql this columt has type timestamp
but in oracle - date
. When I try start server with postgeSQL
I get error:
wrong column type encountered in column [bill_date] in table [charges]; found [timestamp (Types#TIMESTAMP)], but expecting [date (Types#DATE)]
if I add annotation @Temporal(TemporalType.DATE)
I get another error:
Caused by: org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property
But I do not want use java.util.Date
and java.util.Calendar
. How to solve this problem?
Upvotes: 6
Views: 12189
Reputation: 1405
Both Date in Oracle and Timestamp in PostgreSQL store date and time. Indeed according to PostgreSQL Documentation you should map them either to LocalDateTime if the timestamp is w/o the timezone, or to OffsetDateTime if it's with the timezone.
Hibernate 5 should support Java8 Time API, anyway if you're using JPA, you could implement AttributeConverter
import javax.persistence.AttributeConverter;
import java.sql.Timestamp;
import java.time.LocalDate;
public class LocalDateConverter implements AttributeConverter < LocalDate, Timestamp > {
@Override
public Timestamp convertToDatabaseColumn(LocalDate attribute) {
return attribute != null ? Timestamp.valueOf(attribute.atStartOfDay()) : null;
}
@Override
public LocalDate convertToEntityAttribute(Timestamp dbData) {
return dbData != null ? dbData.toLocalDateTime().toLocalDate() : null;
}
}
Then you can use
@Convert(converter = LocalDateConverter.class)
@Column(name = "BILL_DATE")
private LocalDate billDate;
Upvotes: 9