无法使用hibernate PostgreSQL存储java.time.ZonedDateTime

pcj*_*pcj 5 java postgresql spring hibernate

我有一个实体,它扩展了一个名为AbstractAuditingEntity的审计实体类,其中一个字段是

@CreatedDate
@Column(name = "created_date", nullable = false)
@JsonIgnore
private ZonedDateTime createdDate
Run Code Online (Sandbox Code Playgroud)

上面的字段已映射到"created_date"以type 命名的数据库字段"timestamp without time zone".

但在保存此实体时,PostgresSQL会抛出错误,如下所示:

Caused by: org.postgresql.util.PSQLException: ERROR: column "created_date" is of type timestamp without time zone but expression is of type bytea
  Hint: You will need to rewrite or cast the expression.
Run Code Online (Sandbox Code Playgroud)

我在这里寻找相同的错误并找到了解决方案:Hibernate支持的Postgresql UUID?

但解决方案是java.util.UUID,在解决方案中,建议@Type(type="pg-uuid")在具有UUID类型的字段上添加注释 .

有没有像任何这样的愿意使用的类型值pg-uuidZonedDateTime?hibernate @Type注释的不同值常量的任何引用链接?

或者我应该写一个自定义反序列化器类?如何编写这样的反序列化类任何引用链接?

PostgresSQL Version  : 9.6,
<liquibase-hibernate5.version>3.6</liquibase-hibernate5.version>
Run Code Online (Sandbox Code Playgroud)

tep*_*pic 5

对于Hibernate 5.2,它应该开箱即用。

对于5.0,您可以添加一个依赖项来支持它:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>${hibernate.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

对于4.3,可以使用JPA 2.1转换器:

@Converter
public class TimestampConverter implements AttributeConverter<ZonedDateTime, Timestamp> {
    @Override
    public Timestamp convertToDatabaseColumn(ZonedDateTime zonedTime) {
        if (zonedTime == null) {
            return null;
        }
        return new Timestamp(zonedTime.toInstant().toEpochMilli());
    }

    @Override
    public ZonedDateTime convertToEntityAttribute(Timestamp localTime) {
        if (localTime == null) {
            return null;
        }
        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(localTime.getTime()), ZoneId.systemDefault());
    }
}
Run Code Online (Sandbox Code Playgroud)

然后用注释您的属性@Convert

@Convert(converter = TimestampConverter.class)
private ZonedDateTime createdDate;
Run Code Online (Sandbox Code Playgroud)

我注意到您的数据库列为TIMESTAMP。确实应该是TIMESTAMP WITH TIME ZONE,特别是如果您的客户所在时区具有夏令时更改。