[英]JPA @Version in @Embeddable class
when I use the JPA @Version annotaton in an @Embeddable I get the following exception pointing at my Updateable
class: 当我在@Embeddable中使用JPA @Version注释时,出现以下异常,指向我的Updateable
类:
org.hibernate.AnnotationException: Unable to define @Version on an embedded class
Here is my code: 这是我的代码:
@Embeddable
public class Updateable {
@Version
private long modcount;
private String updatedBy;
private DateTime updatedAt;
// getters & setters
}
@Entity
public class SomeEntity {
@Id
private Long id;
@Embedded
private Updateable updateAudit;
// other stuff
}
Is it not possible to have a @Version in an @Embeddable, or is this Hibernate specific? 是否不能在@Embeddable中使用@Version,还是此Hibernate特定于?
An embeddable class is just a convinience way of declaring reusable entity elements, ie your Updateable
could be used in other entities without having to add the fields and the mapping again. 可嵌入类只是声明可重用实体元素的一种便捷方式,即,您的Updateable
可以在其他实体中使用,而不必再次添加字段和映射。
As such, embeddables are part of the entity (as the name suggests they are embedded) and thus independent versioning doesn't make sense. 因此,可嵌入对象是实体的一部分(顾名思义,它们是嵌入式的),因此独立版本控制没有意义。
Adding the @Version
annotation to the embeddable only would also not make much sense since the embeddable itself can't be versioned and you'd have to deal with cases where multiple embeddables are contained in a single entity (eg which version should be used in that case?). 仅将@Version
批注添加到可嵌入对象也没有多大意义,因为可嵌入对象本身无法进行版本控制,并且您必须处理单个实体中包含多个可嵌入对象的情况(例如,应在哪个实体中使用哪个版本这种情况?)。 So since @Version
only makes sense for entities it's easier to just allow that annotation for entities or mapped superclasses. 因此,由于@Version
仅对实体有意义,因此为实体或映射的超类仅允许该注释会更容易。
Actually although the JPA spec recommends that version properties are numeric, strings or timestamps Hibernate seems to provide user defined version types : 实际上,尽管JPA规范建议版本属性为数字,但字符串或时间戳记Hibernate似乎提供了用户定义的版本类型 :
The version column may be a numeric (the recommended solution) or a timestamp. 版本列可以是数字(建议的解决方案)或时间戳。 Hibernate supports any kind of type provided that you define and implement the appropriate UserVersionType. Hibernate支持任何类型的类型,只要您定义和实现适当的UserVersionType即可。
So what you might be able to do (not tested, just derived from the docs) if you want to use Updateable
as your version is to provide a custom user type for Updateable
and then use it like this: 因此,如果您想使用Updateable
作为您的版本,您可能能够做(未经测试,仅从文档派生)是为Updateable
提供自定义用户类型,然后像这样使用它:
@Entity
public class SomeEntity {
@Id
private Long id;
@Type( "your.custom.UserVersionType" )
@Version
private Updateable updateAudit;
// other stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.