This is the code structure that I'm working on.
@Data
public class ParentKey implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator= "uuid")
@GenericGenerator(name="uuid", strategy="uuid2")
private String Pa;
@Id
private String Pb;
// ignoring getters and setters and hashcode and equals()
}
@Entity
@IdClass(ParentKey.class)
@Table(name="parent_demo")
public class Parent_Demo {
@Id
private String Pa;
@Id
// @Column(name="p_b")
private String Pb;
@OneToMany(cascade=CascadeType.ALL,orphanRemoval=true )
@JoinColumn(name="p_a_fk", referencedColumnName="Pa")
@JoinColumn(name="p_b_fk", referencedColumnName="Pb")
private List<Child_Demo> childs = new ArrayList<>();
private String pc;
// ignoring getter and setters
}
@Data
public class ChildKey implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private String ca;
@Id
private String cb;
@Id
private String Pa;
}
@Entity
@IdClass(ChildKey.class)
@Table(name="child_demo")
public class Child_Demo {
@Id
private String Pa; // can i link this value to the value of Pa in ParentKey which is being autogenerated
@Id
private String ca;
@Id
private String cb;
private String cc;
private String cd;
I want to create one to many unidirectional mapping in which the parent's one part of primary key is being auto-generated and the other part is being sent in the Request Body during api call. Similarly , ca and cb will be sent in the Request Body during api call. I want to ensure that Pa also becomes part of composite primary key of the child. the code below generates 2 tables parent_demo with (pa,pb) as primary key and child_demo with (pa,ca,cb) as primary key. However, on doing the post api call, I get the violating not null constraint because pa from child_demo is null. I want to copy the value of pa (autogenerated ) in parent_demo to pa in child_demo.
Can someone help ?