Eclipse error when using multiple @Id for composite primary keys
Asked Answered
C

2

16

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:

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.

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.

Cherian answered 11/1, 2014 at 15:31 Comment(3)
I did not know it is possible in Hibernate 4 :) Found out from the question. I was going to propose the EmbeddedId with an Embeddable class.Ramentum
JPA Facet is not a part of JBoss Tools, it comes form Eclips Daly project which is in turn part of Webtools project. JBoss Tools is based on Webtools. You can ask questions like this on JBoss Tools Users Forum here - community.jboss.org/en/tools.Statist
so you need IdClass, as per the JPA spec thenMart
E
33

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

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).

Edin answered 9/1, 2015 at 18:24 Comment(3)
Thanks, i don't use eclipse anymore, switched to a better IDE. But i think your answer will help other people.Cherian
Its good explanation. Although on MAC STS, its not solving the issue.Hofuf
Thanks. This also helped in our project which uses EclipseLink instead of Hibernate.Adamina
P
2

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;
    }

}
Palmieri answered 15/1, 2014 at 10:36 Comment(2)
Op specifically asked for the new way without a composite key.Tunisia
This doesn't really answer this specific question, but I came across this question/answer when searching for the error, and how to create an id class was what I was looking for. If anyone else is in the same boat, this other answer provides a pretty good id class example: https://mcmap.net/q/149510/-which-annotation-should-i-use-idclass-or-embeddedidAzal

© 2022 - 2024 — McMap. All rights reserved.