I am using JPA (Hibernate) and trying to persist entire new entity with childs and composite keys, but when persisting childs i getting null in key. Table structure:
CREATE TABLE IDENTITY_DOCS
(
PERSON_ID BIGINT NOT NULL,
DOC_TYPE_ID BIGINT NOT NULL,
...
);
CREATE TABLE DOC_TYPES
(
DOC_TYPE_ID BIGINT PRIMARY KEY NOT NULL,
...
);
CREATE TABLE PERSONS
(
PERSON_ID BIGINT PRIMARY KEY NOT NULL,
...
);
Mappings:
@IdClass(...IdentityDocsPK.class)
@Table(name = "IDENTITY_DOCS")
@Entity
public class IdentityDocs {
@Id
@Column(name = "PERSON_ID", nullable = false)
private Long personId;
@Id
@Column(name = "DOC_TYPE_ID")
private Long docTypeId;
@ManyToOne
@MapsId("personId")
@JoinColumn(name = "PERSON_ID", referencedColumnName = "PERSON_ID")
private Employee employee;
...
}
public class IdentityDocsPK implements Serializable {
private Long personId;
private Long docTypeId;
...
}
@Table(name = "PERSONS")
@Entity
@Where(clause = "PERSON_TYPE_ID = 1")
public class Employee {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PERSON_ID")
@Id
private Long personId;
...
@OneToMany(mappedBy = "employee", fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
private List<IdentityDocs> identityDocs;
}
@Table(name = "DOC_TYPES")
@Entity
public class DocTypes {
@Id
@Column(name = "DOC_TYPE_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long docTypeId;
...
}
Test case:
Employee employee = new Employee();
employee.setFirstName("AAAA");
employee.setLastName("BBBB");
employee.setPersonNumber("1337");
IdentityDocs docs1 = new IdentityDocs();
docs1.setDocTypeId(1L); // already in database
docs1.setDocNumber("11111");
docs1.setDocSeries("11111");
docs1.setPlaceIssue("1111");
employee.setIdentityDocs(new ArrayList<IdentityDocs>());
employee.getIdentityDocs().add(docs1);
em.persist(employee);
Error code:
NULL not allowed for column "PERSON_ID"; SQL statement:
insert into IDENTITY_DOCS (DATE_EXPIRED, DATE_ISSUE, DOC_NUMBER, DOC_SERIES, PLACE_ISSUE, DOC_TYPE_ID, PERSON_ID) values (?, ?, ?, ?, ?, ?, ?)
I also tried without @MapsId (using insertable = false, updatable=false on relevant field), but with same result. Question @OneToMany and composite primary keys? is relevant, but i dont find answer there