0

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 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 Child1 and Child2. Child2 has ManyToOne relation with Child1 i.e Child2 contains the primary key of Child1 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 Child1 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 Child2 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 Child1 associated with Child2 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:

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;
    }
}
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • The problem IMHO is the merge method. HIbernate doesn't work that well when using merge. Hibernate doesn't actually work that well when dealing with detached entities. I mean, it has this feature and it may help in many simpler situations. But it doesn't really rock. We had this issue in our project as well. Then we changed everything to using DTOs. Everything works much smoother now. – Stefan Steinegger Jul 08 '16 at 05:54
  • Yes in my opinion as well DTO would be useful. Your core logic should represent the domain and when the client i.e. web needs some data which is quiet different in view as compared to domain then use Data Transfer Object to handle the situation. – Narendra Pathai Jul 10 '16 at 09:05
  • How can you use DTOs to update some columns in DB for same parent child scenario? Eg please? – coretechie Apr 04 '18 at 07:39

1 Answers1

0

As I said in my comment, using merge is not ideal.

Anyway, you could work around this by merging some children first.

// sorry if this is not correct java, I'm a C# programmer
foreach(Child2 child2 in parent.Children2)
{
  session.merge(child2);
}
session.merge(parent);

By the way, you only need merge if the object is already in the session. If it is guaranteed not to be, just use saveOrUpdate.

session.saveOrUpdate(parent);
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193