简体   繁体   English

为什么我不能覆盖和注释实体映射的getter方法?

[英]Why can I not override and annotate a getter method for entity mapping?

Given this class: 给定此类:

@MappedSuperclass
public abstract class AbstractEntity {

    int id;
    public void setId(int id) { this.id = id; }
    public int getId() { return id; }

    // other mappings
}

I want to define an entity: 我想定义一个实体:

@Entity
public class SomeEntity extends AbstractEntity {

    @Override
    @Id // or @OneToOne etc.
    public int getId() { return id; }
}

But this fails with a "No identifier specified" (or a "Could not determine type for") error on SomeEntity. 但这失败,并在SomeEntity上出现“未指定标识符”(或“无法确定类型”)错误。 If I remove the getter from the superclass it works. 如果我从超类中删除吸气剂,它将起作用。 Can't I do this override strategy? 我不能执行此替代策略吗? Why not, or if yes - how? 为什么不呢?或者,如果可以,怎么办?

Adding 新增中

@AttributeOverride(name = "id", column = @Column(name = "ID"))

to the subclass does not change the error. 子类不会更改错误。

For you to create an entity class there are requirements that the class must meet. 要创建实体类,必须满足该类的要求。 Ex. 例如 must have a public/private constructor. 必须具有公共/私有构造函数。

Here is the list of requirements: 以下是要求列表:

http://docs.oracle.com/javaee/5/tutorial/doc/bnbqa.html http://docs.oracle.com/javaee/5/tutorial/doc/bnbqa.html

Hope this helps. 希望这可以帮助。

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

相关问题 我可以在没有映射到数据库的实体 class 中有一个方法吗 - can i have a method in entity class without Mapping to the database 我可以在Spring中将方法注释为可缓存的吗? - Can I annotate a method as cacheable in Spring? 为什么我可以用公共方法覆盖受保护的方法? - Why can I override a protected method with public method? 为什么我不能使用class的实现作为参数覆盖方法 - Why I can not override method using implementation of class as parameter 覆盖方法,为什么我不能引用新的自己的方法? - Override method, why can't I referenciate new own methods? 为什么当我使用JPA双向映射到一对一时,我不能从一个直接持久化实体 - why when i use JPA bidirectional mapping one to one i can not persist entity from one direct 如何为多维数组创建getter方法和setter方法? - How can I create a getter method and a setter method for a multidimensionnal array? 当我覆盖equals()方法时,为什么要覆盖hashCode()? - Why should I override hashCode() when I override equals() method? JavaFx-为什么不能覆盖方法“ updateItem”? - JavaFx - why can't Override the method “updateItem”? 为什么我们需要使用@Entity注释POJO - Why do we need to annotate a POJO with @Entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM