10

my id class as follows,

public class EmployeeId implements Serializable{
    public EmployeeId(){}
    public EmployeeId(Integer id, String country){
        this.id = id;
        this.country = country;
    }

    private Integer id;
    private String country;

    @Override
    public int hashCode(){        
        return this.getCountry().hashCode() + getId();
    }

    @Override
    public boolean equals(Object o){
        boolean flag = false;
        EmployeeId myId = (EmployeeId) o;

        if((o instanceof EmployeeId) 
                && (this.getCountry().equals(myId.getCountry()))
                && (this.id == myId.getId())){
            flag = true;
        }
        return flag;
    }
// rest of the code with getters only
}

Following is my entity using

@Entity
@IdClass(EmployeeId.class)
@Table(name="TBL_EMPLOYEE_FOUR")
public class EmployeeEntityTwo {

    public EmployeeEntityTwo(){}
    public EmployeeEntityTwo(Integer id,String country, String empName){
        this.country = country;
        this.employeeId = id;
        this.empName = empName;
    }

    @Id
    @Column(name="ID")
    private Integer employeeId;

    @Id
    @Column(name="COUNTRY",length=50)
    private String country;

    @Column(name="NAME",length=50)
    private String empName;
// getters and setters
}

This is my table

create table TBL_EMPLOYEE_FOUR(
    ID integer, 
    COUNTRY varchar(50),
    NAME varchar(50),
    constraint PK_EMP_00239 primary key(ID,COUNTRY)
)

This is what i am trying to run

 private static void idClassStore(EntityManager em) throws Exception{
     List<EmployeeEntityTwo> employees = Arrays.asList(new EmployeeEntityTwo(12, "KENYA", "Ridushi Ogambe"),
                                                       new EmployeeEntityTwo(13, "GHANA", "Mikila Hanza"),
                                                       new EmployeeEntityTwo(14, "EGYPT", "Abdul Hameed Fagdaul"),
                                                       new EmployeeEntityTwo(15, "MOROCCO", "Jamil Mahmoud"),
                                                       new EmployeeEntityTwo(16, "LIBERIA", "Robert Damus"));

     for(EmployeeEntityTwo employee : employees){
         em.persist(employee);            
       }
}

But i get an exception as

Caused by: org.hibernate.AnnotationException: Property of @IdClass not found in entity com.entities.EmployeeEntityTwo: id

I am using JPA with Hibernate as persistence provider,

But @IdClass which i have used is of import javax.persistence.IdClass;

so where is am going wrong,

1 Answer 1

15

I have discovered the solution:

The 'id' field in the classes EmployeeEntityTwo and EmployeeId should be same.

// EmployeeId.java;
private Integer id;

should be

// EmployeeId.java;
private Integer employeeId;

I adjusted the getter respectively, and it worked.


From javax.persistence.IdClass JavaDocs:

The names of the fields or properties in the primary key class and the primary key fields or properties of the entity must correspond and their types must be the same.

4
  • Hit the checkmark next to your answer, so that it's clear this question is answered.
    – sharakan
    Commented Mar 26, 2013 at 15:53
  • i can't, it says i can answer my own question after two days Commented Mar 26, 2013 at 15:54
  • I got the same error. Error message does not match the actual issue. Very ambiguous. Commented Apr 21, 2017 at 11:19
  • Another answer gives some hint. The problem arises from annotations. stackoverflow.com/a/3774245/10809764
    – Sean
    Commented May 2, 2020 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.