简体   繁体   English

JPA实体可以从接口方法继承@Version吗?

[英]Can JPA entity inherit @Version from an interface method?

I defined a Versioned interface, expecting that I could implement it on any persisted entity that I wanted to use the JPA versioning features (eg, comparing version for mid-air collisions, auto-incrementing on updates). 我定义了一个Versioned接口,期望我可以在任何我希望使用JPA版本控制功能的持久化实体上实现它(例如,比较版本用于空中碰撞,自动递增更新)。

public interface Versioned {
    @Version
    int getRevision();
}

This doesn't save much (if any) coding, but it also gives me a marker interface that I had plans for later. 这不会节省太多(如果有的话)编码,但它也为我提供了一个标记界面,我稍后计划。

public class Foo implements Versioned {
    private int revision;

    @Override
    public int getRevision() {
            return revision;
    }
}

@Version is allowed on methods, and I expected Foo to inherit that characteristic from the Versioned interface. 方法允许@Version ,我希望Foo从Versioned接口继承该特性。 That does not appear to be the case. 情况似乎并非如此。 I can update my instance of Foo in the database, but the revision number doesn't change automatically. 我可以在数据库中更新我的Foo实例,但修订版号不会自动更改。

If, however, I add a @Version annotation to the implementation, it works as expected: 但是,如果我在实现中添加@Version注释,它将按预期工作:

    @Version
    private int revision;

I'm not stuck - I can get the functionality I need. 我没有被卡住 - 我可以获得我需要的功能。 I'm just curious: is there a "trick" to getting this to work properly with the annotation on the interface's method? 我只是好奇:是否有一个“技巧”让这个与界面的方法上的注释正常工作? Or can someone point me to documentation of why this doesn't work as I expect? 或者有人可以指出我为什么这不起作用的记录?

Annotation inheritance doesn't work on methods. 注释继承不适用于方法。 From the documentation of @Inherited : 来自@Inherited文档

Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. 请注意,如果使用带注释的类型来注释除类之外的任何内容,则此元注释类型不起作用。 Note also that this meta-annotation only causes annotations to be inherited from superclasses; 另请注意,此元注释仅导致注释从超类继承; annotations on implemented interfaces have no effect . 已实现接口上的注释无效

I'm pretty sure this isn't possible. 我很确定这是不可能的。

First of all, because @MappedSuperclass is needed to inherit mapping annotations from a superclass. 首先,因为需要@MappedSuperclass来继承超类中的映射注释。 So I don't see why mapping annotations would automatically be inherited from an interface. 所以我不明白为什么映射注释会自动从接口继承。

Second because if the JPA anotations in the class are on fields and not on getters, the annotations on getters will be ignored. 其次,因为如果类中的JPA anotations在字段上而不在getter上,则getter上的注释将被忽略。

Paragraph 2.11.2 of the spec says: 规范第2.11.2段说:

An entity may inherit from a superclass that provides persistent entity state and mapping information, but which is not itself an entity. 实体可以从提供持久实体状态和映射信息的超类继承,但它本身不是实体。 Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes. 通常,这种映射的超类的目的是定义多个实体类共有的状态和映射信息。

A mapped superclass, unlike an entity, is not queryable and must not be passed as an argument to EntityManager or Query operations. 与实体不同,映射的超类不可查询,不能作为参数传递给EntityManager或Query操作。 Persistent relationships defined by a mapped superclass must be unidirectional. 映射的超类定义的持久关系必须是单向的。

Both abstract and concrete classes may be specified as mapped superclasses . 抽象类和具体类都可以指定为映射的超类 The MappedSuper- class annotation (or mapped-superclass XML descriptor element) is used to designate a mapped superclass. MappedSuper-类注释(或mapped-superclass XML描述符元素)用于指定映射的超类。

(emphasis mine) (强调我的)

This is in the chapter about inheritance, and nowhere does the spec talsk about inherited mapping from interfaces. 这是关于继承的章节,并且没有关于从接口继承映射的spec talsk。 Mapping can only be inherited from abstract or concrete classes. 映射只能从抽象类或具体类继承。

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

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