I have in my data base one table with primary composite key, when I model it with Hibernate I use @EmbeddedId
& @Embedable
. One column of this composite primary key have a generated value with @GeneratedValue(strategy = GenerationType.IDENTITY)
.
When I try to create two new object in my data base I have an error like this :
org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [MyPackage#MyEmbedableClass [MyGeneratedPrimaryKeyValue=null, OtherPrimaryKey=21]]
But when I look in my data base my objects have been created. I don't understand my mistakes and I don't know how solve my problem.
I have found some topics like mine but I haven't found answer to my question.
@Entity(name = "Certification")
@Table(name = "CERTIFICATION")
public class Certification implements Serializable {
static final long serialVersionUID = -4399907743392740963L;
@EmbeddedId
private CertificationPK certificationPK;
// Others variables
// constructors
// Getter / Setter
// toString
// hashCode / equals
}
@Embeddable
public class CertificationPK implements Serializable {
private static final long serialVersionUID = 1433990897506209802L;
// MyGeneratedPrimaryKeyValue=null when I create
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotNull
@Column(name = "CERTIFICATION_ID")
private Integer certificationId;
// Other variable
// constructors
// Getter / Setter
// toString
// hashCode / equals
}
Thanks in advance for your help.