Does hibernate allow to define the order of saving child elements
<p>Does hibernate allow to define the order of executing child elements defined as List in a POJO.
Suppose we have one <code>Parent</code> class that contains multiple children(<code>Child1</code> , <code>Child2</code>). The association between parent and child is OneToMany and they are defined as list.
We are having bi-directional relation between parent-child.
There is also ManyToOne relation between <code>Child1</code> and <code>Child2</code>. <code>Child2</code> has ManyToOne relation with <code>Child1</code> i.e <code>Child2</code> contains the primary key of <code>Child1</code> as a foreign key reference and both of them shares OneToMany relation with <code>Parent</code>.</p>

<p>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 <code>Child1</code> and <code>Child2</code> 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 <code>Child2</code> and then it inserts <code>Child1</code>. Ideally it should insert <code>Parent</code>, <code>Child1</code> and <code>Child2</code>. So it can manage association between <code>Child1</code> and <code>Child2</code>.</p>

<p>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.</p>

<p>we have also deleted the foreign key reference of <code>Child1</code> associated with <code>Child2</code> from Database. But hibernate automatically manages the relation so it does not work out.</p>

<p>There is only one requirement of changing the insertion order of the Children defined in <code>Parent</code> Class</p>

<p>Kindly provide the suggestions/ solution ASAP</p>

<p>Code Snippet:</p>

<pre><code>public class Parent{
    private List&lt;Child1&gt; child1;
    private List&lt;Child2&gt; 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&lt;Child1&gt; getChild1() {
        return child1;
    }

    public void setChild1(List&lt;Child1&gt; child1) {
        this.child1 = child1;
    }


    @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "parent", orphanRemoval = true)
    @Fetch(FetchMode.SUBSELECT)
    public List&lt;Child2&gt; getChild2() {
        return child2;
    }

    public void setChildren2(List&lt;Child2&gt; child2) {
        this.child2 = child2;
    }

}

public class Child1{
    private Parent parent;
    private Set&lt;Child2&gt; 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&lt;Child2&gt; getChild2() {
        return child2;
    }
    public void setChild2(Set&lt;Child2&gt; 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;
    }
}
</code></pre>