Does hibernate allow to define the order of executing child elements defined as List in a Pojo.
Suppose we have one parent class that contains multiple children(Child1 , Child2). The association between parent child is OneToMany and they are defined as list.
We are having bi-directional relation between parent-child.
There is also ManyToOne relation between Child1 and child2.Child 2 has ManyToOne relation with Child1 i.e Child 2 contains the primary key of child 1 as a foreign key reference and both of them shares OneToMany relation with parent.
Lets assume that we have provided all the possible parent-child relation and now we are calling merge method of hibernate session. The problem we are facing here is, the association between child 1 and child2 is not managed. It gives hibernate exception with message "No row with the given identifier exists". this error derives because of hibernate inserts parent then it inserts child 2 and then it inserts child1. Ideally it should insert Parent, child1 and Child2. So it can manage association between child1 and Child2.
Ideally the classes will be fetched as they were defined in session factory or they are defined in hibernate.cfg.xml file. here we have tried to change the order of defining classes in XML. But it results the same.
we have also deleted the foreign key reference of child 1 associated with child 2 from Database. But hibernate automatically manages the relation so it does not work out.
There is only one requirement of changing the insertion order of the Children defined in Parent Class
Kindly provide the suggestions/ solution ASAP
Code Snippet:
Code:
public class Parent{
private List<Child1> child1;
private List<Child2> child2;
public Parent(){
child1 = Collectionz.newArrayList();
child2 = Collectionz.newArrayList();
}
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "parent", orphanRemoval = true)
@Fetch(FetchMode.SUBSELECT)
public List<Child1> getChild1() {
return child1;
}
public void setChild1(List<Child1> child1) {
this.child1 = child1;
}
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "parent", orphanRemoval = true)
@Fetch(FetchMode.SUBSELECT)
public List<Child2> getChild2() {
return child2;
}
public void setChildren2(List<Child2> child2) {
this.child2 = child2;
}
}
public class Child1{
private Parent parent;
private Set<Child2> child2;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="PARENT_ID")
public PkgData getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
@OneToMany(cascade={CascadeType.ALL},fetch=FetchType.LAZY,mappedBy="child1")
@Fetch(FetchMode.SUBSELECT)
public Set<Child2> getChild2() {
return child2;
}
public void setChild2(Set<Child2> child2) {
this.child2 = child2;
}
}
public class child2{
private Parent parent;
private Child1 child1;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="PARENT_ID")
public PkgData getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="CHILD_1_ID")
public Child1 getChild1() {
return child1;
}
public void setChild1(Child1 child1) {
this.child1 = child1;
}
}