Java 对复合主键使用多个 @Id 时出现 Eclipse 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21064138/
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-08-13 06:19:42  来源:igfitidea点击:

Eclipse error when using multiple @Id for composite primary keys

javahibernatejpajboss-tools

提问by Mateus Viccari

in my Model project (it only has the persistent classes, aka java beans) i have a class which has a composite primary key. To map this, i have used two @Id in my class. Before hibernate 4 it was not possible but now it is Ok. So, the problem is, eclipse is showing an error in this class, saying that it should be done in the old way. Like this:

在我的模型项目(它只有持久类,又名 java bean)中,我有一个具有复合主键的类。为了映射这个,我在我的班级中使用了两个@Id。在休眠 4 之前,这是不可能的,但现在可以了。所以,问题是,eclipse在这个类中显示了一个错误,说它应该用旧的方式来完成。像这样:

False error

错误的错误

As i said, it is a false error, because the code works fine if i execute it. I have JBoss Tools plugin installed on eclipse, but i don't know if the errors is being caused by it or by eclipse.

正如我所说,这是一个错误的错误,因为如果我执行它,代码就可以正常工作。我在 eclipse 上安装了 JBoss Tools 插件,但我不知道错误是由它还是由 eclipse 引起的。

Anyone know how to solve this issue? Not that it is preventing me from executing the app, but it is an annoying thing to have the error being always shown.

有谁知道如何解决这个问题?并不是说它阻止我执行应用程序,而是始终显示错误是一件令人讨厌的事情。

--- EDIT ---

- - 编辑 - -

So, now i know the problem is on JBoss Tools because i deactivated the JPA facet on the project and the error have stopped. But i wish i could use the facilities that JBoss Tools gives, so... no solution yet.

所以,现在我知道问题出在 JBoss 工具上,因为我停用了项目中的 JPA 方面并且错误已经停止。但我希望我可以使用 JBoss Tools 提供的工具,所以...还没有解决方案。

采纳答案by petroman

Well it's almost a year late, but I just came across this problem myself today :-)

好吧,已经晚了将近一年,但我今天才遇到这个问题:-)

You can turn off this error in Eclipse. Go to

您可以在 Eclipse 中关闭此错误。去

Preferences->Java Persistence->JPA->Errors/Warnings

Preferences->Java Persistence->JPA->Errors/Warnings

Under the Type section look for the category "ID class must be used when multiple ID mappings defined." and change it from Error to Ignore (or whatever severity you want to give it).

在类型部分下查找类别“定义多个 ID 映射时必须使用 ID 类”。并将其从“错误”更改为“忽略”(或您想要赋予的任何严重性)。

回答by BaN3

well if you have a composite key u should also have a composite key class

好吧,如果你有一个复合键,你也应该有一个复合键类

something mapped like this:

像这样映射的东西:

@Entity
@IdClass(PK_BP.class)
@Table(name="BP_BIS")
public class BP_BIS implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id  
    private String BP_MODE;
    @Id  
    private String BP_BD;

the composite key class will be like this :

复合键类将是这样的:

public class PK_BP implements Serializable
{
    private static final long serialVersionUID = 1L;

    private String BP_MODE;
    private String BP_BD;

    public PK_BP()
    {}

    public PK_BP(String bP_MODE, String bP_BD) {
        this.BP_MODE = bP_MODE;
        this.BP_BD = bP_BD;
    }

}