-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: execution order for Childrens defined in Pojo
PostPosted: Fri Jul 08, 2016 9:38 am 
Newbie

Joined: Wed Jul 06, 2016 2:43 am
Posts: 2
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;
   }

}


Top
 Profile  
 
 Post subject: Re: execution order for Childrens defined in Pojo
PostPosted: Mon Jul 11, 2016 6:01 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Can you provide the data access code and paste the exact order of statements that Hibernate?


Top
 Profile  
 
 Post subject: Re: execution order for Childrens defined in Pojo
PostPosted: Mon Jul 11, 2016 8:49 am 
Newbie

Joined: Wed Jul 06, 2016 2:43 am
Posts: 2
Code:
Parent parent = new Parent();
List<Child1> children1 = new ArrayList<Child1>();
Child1 c1 = new Child1();
c1.setParent(parent);
   
Child1 c2 = new Child1();
c2.setParent(parent);

children1.add(c1);
children1.add(c2);

parent.setChild1(children1);

List<Child2> children2 = new ArrayList<Child2>();
Child2 ch1 = new Child2();
//association between child 2 and child 1   
ch1.setChild1(c1);
ch1.setParent(parent);
   
Child2 ch2 = new Child2();
ch2.setParent(parent);
//association between child 2 and child 1   
ch2.setChild1(c2);


children2.add(ch1);
children2.add(ch2);

parent.setChild1(children1);
parent.setChild2(children2);

session.merge(parent);


Here hibernate takes child 2 first for Parent then how we can change hibernate's query execution order


Top
 Profile  
 
 Post subject: Re: execution order for Childrens defined in Pojo
PostPosted: Mon Jul 11, 2016 10:09 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Why do you call merge instead of persist? Persist is meant for changing the entity state from TRANSIENT to MANAGED.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.