eclipse @Entity 无法识别 @MappedSuperclass 中的 @Id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12135233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:51:55  来源:igfitidea点击:

@Entity not recognizing the @Id in a @MappedSuperclass

javaeclipsehibernatejpajpa-2.0

提问by Gordon

I'm attempting to create a base class for a set of entities to reduce coding effort and duplication. My thought is that the base class has the common meta-data fields, and the child classes deal with their unique attributes.

我正在尝试为一组实体创建一个基类,以减少编码工作和重复。我的想法是基类具有公共元数据字段,子类处理它们的唯一属性。

My base class:

我的基类:

@MappedSuperclass
public abstract class FinanceEntityBean {
    protected Long id;

    @Version
    private long version;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(final Long id) {
        this.id = id;
    }
}

The first entity:

第一个实体:

@Entity
@Table(name = "tag")
public class Tag extends FinanceEntityBean {
}

I've written tests using this code to do CRUD functions on the Tag entity, and they are all working fine.

我已经使用此代码编写了对 Tag 实体执行 CRUD 功能的测试,并且它们都运行良好。

My question is - why does Eclipse (Indigo) insist that Taghas an error:

我的问题是 - 为什么 Eclipse (Indigo) 坚持认为Tag有错误:

The entity has no primary key attribute defined

The entity has no primary key attribute defined

I've changed that to a warning for now so my code will compile, but I'm curious why Eclipse isn't happy, and if I've misunderstood something.

我现在已将其更改为警告,以便我的代码可以编译,但我很好奇 Eclipse 为何不高兴,以及我是否误解了某些内容。

Is this valid JPA 2.0 code? Hibernate 4.1.5 is my JPA provider.

这是有效的 JPA 2.0 代码吗?Hibernate 4.1.5 是我的 JPA 提供程序。

采纳答案by Karen Butzke

When using mixed access you have to specify the access type. See Eclipse Dali bug 323527for giving a better validation error when both fields and properties are annotated.

使用混合访问时,您必须指定访问类型。请参阅 Eclipse Dali错误 323527,以在注释字段和属性时提供更好的验证错误。

Option 1 : Annotate the getVersion() method instead, only properties are annotated.
Option 2 : Specify mixed access type as follows:

选项 1:改为注释 getVersion() 方法,仅注释属性。
选项 2:指定混合访问类型如下:

@MappedSuperclass
@Access(AccessType.PROPERTY)
public abstract class FinanceEntityBean {
    protected Long id;

    @Version
    @Access(AccessType.FIELD)
    private long version;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(final Long id) {
        this.id = id;
    }
}

回答by Brian Vosburgh

If FinanceEntityBeanis defined in a different Eclipse project from Tag, you may be suffering from the Dali bug "No primary key attribute in other plug-in project".

如果FinanceEntityBean是在不同的 Eclipse 项目中定义的Tag,您可能会遇到 Dali 错误 “在其他插件项目中没有主键属性”

The workaround is to list FinanceEntityBeanin the persistence.xmlfile associated with Tag.

解决方法是FinanceEntityBeanpersistence.xmlTag.

回答by calvinkrishy

I am fairly certain those are valid mappings.

我相当确定那些是有效的映射。

The JPA 2.0 spec provides this example when talking about MappedSuperClasses (section 2.11.2):

JPA 2.0 规范在讨论 MappedSuperClasses 时提供了这个例子(第 2.11.2 节):

@MappedSuperclass 
public class Employee {
    @Id protected Integer empId; 
    @Version protected Integer version; 
    @ManyToOne @JoinColumn(name="ADDR") protected Address address;
    public Integer getEmpId() { ... } 
    public void setEmpId(Integer id) { ... } 
    public Address getAddress() { ... } 
    public void setAddress(Address addr) { ... }
}

// Default table is FTEMPLOYEE table 
@Entity public class FTEmployee extends Employee {
    // Inherited empId field mapped to FTEMPLOYEE.EMPID 
    // Inherited version field mapped to FTEMPLOYEE.VERSION 
    // Inherited address field mapped to FTEMPLOYEE.ADDR fk
    // Defaults to FTEMPLOYEE.SALARY protected Integer salary;
    public FTEmployee() {}
    public Integer getSalary() { ... } 
    public void setSalary(Integer salary) { ... }
}

回答by Bryan Pugh

I had the same problem, but for a different reason, but I didn't realize it. In further research I found that in my case I had the MappedSuperclass in a different jar. According to User Gas https://stackoverflow.com/users/3701228/gas:

我遇到了同样的问题,但出于不同的原因,但我没有意识到。在进一步的研究中,我发现在我的情况下,我在不同的 jar 中有 MappedSuperclass。根据用户气体https://stackoverflow.com/users/3701228/gas

"According to JPA spec, if you have jpa classes in separate jar you should add it to the persistence.xml (I don't know, if Hibernate requires that, but you can give it a try). Try to add following entry to your persistence.xml entity.jar"

“根据 JPA 规范,如果您在单独的 jar 中有 jpa 类,则应将其添加到 persistence.xml(我不知道,如果 Hibernate 需要这样做,但您可以尝试一下)。尝试将以下条目添加到你的persistence.xml entity.jar”

He references what is the right path to refer a jar file in jpa persistence.xml in a web app?as a description on how to do this.

他引用了在 Web 应用程序中引用 jpa persistence.xml 中的 jar 文件的正确路径是什么?作为有关如何执行此操作的说明。

回答by Raymond Naseef

I know this is late, but it is still an issue. Thank you @Kemoda stating in a comment to the question, this can be turned off in Eclipse preferences.

我知道这已经晚了,但这仍然是一个问题。谢谢@Kemoda 在对问题的评论中指出,这可以在 Eclipse 首选项中关闭。

Easiest way to "correct" this is to go set preference as you see fit for your environment. I like "Warning" because in cases where the entity does not have a primary key is an error situation.

“纠正”这一点的最简单方法是根据您认为适合您的环境来设置首选项。我喜欢“警告”,因为在实体没有主键的情况下是一种错误情况。

JPA Errors/Warnings Type Entity has no primary key: [Error|Warning|Info|Ignore]

JPA 错误/警告类型实体没有主键:[错误|警告|信息|忽略]