3

I'm trying to persist ZonedDateTime to Oracle. Following is my domain entity class:

@Entity
@Table(name = "TESTTABLE")
public class Order {
    private static final long serialVersionUID = 1L;

    @Type(type = "uuid-binary")
    @Column(name = "ID")
    private UUID id;

    @Column(name = "CREATE_DATE")
    private ZonedDateTime createdOn;

..and so on.

I also have a converter as follows to convert the dates:

@Converter(autoApply = true)
public class OracleZonedDateTimeSeriliazer implements AttributeConverter<ZonedDateTime,Date>{


        @Override
    public Date convertToDatabaseColumn(ZonedDateTime attribute) {
        return attribute == null ? null : java.util.Date.from(attribute.withZoneSameInstant
                (ZoneOffset.UTC).toInstant());
    }

    @Override
    public ZonedDateTime convertToEntityAttribute(Date dbData) {
        return dbData == null ? null : ZonedDateTime.ofInstant(dbData.toInstant(), DateUtils.getEasternZoneId());
    }
}

When i try to persist this entity I'm getting the following stacktrace:

2016-12-16 10:47:06,669 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-00932: inconsistent datatypes: expected DATE got BINARY
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement

If anyone could help me what i'm doing wrong, it'd be much appreciated.

Gurkha
  • 1,104
  • 4
  • 20
  • 37
  • Perhaps you should try converting to `java.sql.Timestamp` instead of `java.util.Date`, given that `java.util.Date` is not supported by the JDBC API, so the converter is not invoked and the `ZonedDateTime` is serialied to bytes as being an "custom" Java class. You could of course also just include `hibernate-java8`, see http://stackoverflow.com/a/33001846/5221149 – Andreas Dec 16 '16 at 15:59
  • What is the DB-column-type of "CREATE_DATE"? – Meno Hochschild Dec 16 '16 at 15:59
  • 1
    @MenoHochschild its DATE which is sql.Date – Gurkha Dec 16 '16 at 16:15
  • The type DATE does not seem to be appropriate for a complex type like `ZonedDateTime` which includes TIME and ZONE. Anyway, you try to map it finally to `Instant`. So you can just use SQL-type TIMESTAMP in the column definition and the JDBC-type `java.sql.Timestamp` inside your converter. Maybe an extra Hibernate @Type-annotation will help, too. – Meno Hochschild Dec 16 '16 at 16:23

1 Answers1

1

Add @Convert(converter=OracleZonedDateTimeSeriliazer.class) on top of your createdOn attribute on ur Entity class.

Sike12
  • 1,232
  • 6
  • 28
  • 53