ZonedDateTime with Oracle

时间:2016-12-16 15:48:57

标签: java hibernate jpa zoneddatetime

我正在尝试将ZonedDateTime持久化到Oracle。以下是我的域实体类:

@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.

我还有一个转换器,如下所示转换日期:

@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());
    }
}

当我试图坚持这个实体时,我得到了以下的堆栈跟踪:

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

如果有人能帮助我,我做错了什么,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

在你的Entity类的createdOn属性之上添加@Convert(converter = OracleZonedDateTimeSeriliazer.class)。