繁体   English   中英

在JPA中将LocalDateTime与@Version注释一起使用

[英]Using LocalDateTime with @Version annotation in JPA

我的Entity类中包含以下字段:

@Version
@Column(name = "updated")
private LocalDateTime lastUpdated;

@Column(name = "created")
private LocalDateTime created;

created字段工作正常,因为我实现了从LocalDateTimeTimestampAttributeConverter

部署到容器时, lastUpdated字段会出现以下错误:

java.lang.ClassCastException: org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter cannot be cast to org.hibernate.type.VersionType

我知道可以通过使用Timestamp作为lastUpdated的变量类型代替LocalDateTime

我想知道是否可以覆盖@Version注释行为以接受LocalDateTime作为变量类型。

尝试hibernate-java8依赖关系,它支持Java 8特定的数据类型。

<!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-java8 -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.1.0.Final</version>
</dependency>

下面来自休眠网站

支持使用Java8特定数据类型,例如域模型中的任何JSR 310日期/时间类型。

VersionType<Instant>作为其中的一部分被添加,因此您可以根据需要使用它

更新1

已在H2 DB上具有hibernate-java8依赖项的LocalDateTime上检查了Version批注,它有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM