Provided id of the wrong type hibernate

I faced the same issue. I had two separate PK classes that had the same fields. So I removed one PK and used only one in the owner and child entity. This solved the problem.


Looks like this is a defect in hibernate version 3.2.6 which is still not resolved. Came across this JIRA.

Having multiple @Id is supported by Hibernate but seems it fails under one to one mapping, suggested way of resolving this is to use single CompositeKey, which means you create a PK class

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class PKClass implements Serializable {

    @Column(name = "NUM")
    private String num;

    @Column(name = "INIT")
    private String init;

    //gettter setter here

}

then in your Entity use this as the ID

public class BEntity implements Serializable{

    @Id
    private PKClass pkClass = null;

    @Column(name = "V_CNT")
    private Integer vcnt;

   //{{{some column omitted}}}//
}

public class AEntity implements Serializable{

    @Id
    private PKClass pkClass = null;

    @OneToOne
    @PrimaryKeyJoinColumns({
        @PrimaryKeyJoinColumn(name="NUM", referencedColumnName="NUM"),
        @PrimaryKeyJoinColumn(name="INIT", referencedColumnName="INIT")
    })
    private BEntity bEntity;
}