I have a parent entity object that contains three lists of child records. Each of the three lists are represented by their own entity object class with the primary key for these child entities being a composite key that contains the parent's unique identity primary key.
Parent
ParentId - Identity/Unique Sequence Column, managed by Hibernate
ChildEntityA
ParentId
RecordType
ChildEntityB
ParentId
oldStatus
newStatus
type
In my code what I want to be capable of doing would be something like:
Code:
Parent p = new Parent();
ChildEntityA a = new ChildEntityA();
ChildEntityB b = new ChildEntityB();
p.addChildA(a);
p.addChildB(b);
getEntityManager().persist(p);
The problem I experience is that the parent's id isn't being set in the child entity objects at the time the records are being persisted. I have seen several articles on this on various web sites and tried them all and yet nothing seems to work.
Here is a brief snap shot of the relationship in the parent class:
Code:
@OneToMany(mappedBy="parent", cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
public List<ChildEntityA> getChildA() {
return childA;
}
public void setChildA(List<ChildEntityA> childA) {
this.childA = childA;
}
@OneToMany(mappedBy="parent", cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
public List<ChildEntityB> getChildB() {
return childB;
}
public void setChildB(List<ChildEntityB> childB) {
this.childB = childB;
}
Child A looks like:
Code:
@Entity
@Table(name="childa")
public class ChildEntityA
{
@Embeddable
public static class Pk implements Serializable {
@Column(name="parent_id",nullable=false,updatable=false)
private Long parentId;
@Column(name="record_type",nullable=false,updatable=false,length=25)
private String recordType;
// getter and setters
}
@EmbeddedId
private Pk id = new Pk();
@Column(name="parent_name",length=25,nullable=false)
private String name;
@ManyToOne
private Parent parent;
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
// getter and setters
}
Can anyone explain what I could be doing incorrectly?