I'm trying to make a composite primary key mapping and doesn't work.
The requisites are:
- The relation may be with
@IdClass
annotation - I need the relationship with person entity be
@ManyToOne
My code:
@Entity
@IdClass(PhonePK.class)
public class Phone implements Serializable{
@Id
@Column(name = "codigo", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long code;
@Id
@ManyToOne
@JoinColumn(name = "person", referencedColumnName = "code", nullable = false)
private Person person;
@Basic
@Column(name = "number", nullable = false)
private Integer number;
//getters and setters
//equals and hashcode
}
public class PhonePK implements Serializable {
private Long code;
private Long person;
public PhonePK(){}
public PhonePK(Long code, Long person) {
this.code = code;
this.person = person;
}
//getters and setters
//equals and hashcode
}
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "code", nullable = false)
private Long code;
//getters and setters
//equals and hashcode
}
They way I'm trying to persist:
//a lot of code
//em = Entitymanager
em.persist(person);
phone.setPerson(person);
em.persist(phone);
The error that I'm receiving is:
Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: No part of a composite identifier may be null