title: Hibernate not updating child object while updating parent object
body: <p>I have two tables parent and child has @oneTomany relationship between them.
  following is my table structure.</p>
  
  <p><strong>Table Structure</strong> </p>
  
  <pre><code>CREATE TABLE `parent` (
    `id_parent` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id_parent`)
  )
  
  CREATE TABLE `child` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) DEFAULT NULL,
    `parent_id` int(3) NOT NULL,
    PRIMARY KEY (`id`),
    KEY `fk_parent_child` (`group_id`),
    CONSTRAINT `fk_parent_child` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id_parent`)
  )
  </code></pre>
  
  <p>I have created Entity classes for this as follows</p>
  
  <p><strong>Parent Class.</strong></p>
  
  <pre><code>@Entity
  @Table(name = "parent")
  public class Parent {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id_parent")
      private int id;
  
      @Column(name = "name")
      private String name;
  
      @OneToMany(mappedBy = "defaultchild", fetch = FetchType.EAGER)
      @Cascade({ CascadeType.SAVE_UPDATE, CascadeType.MERGE })
      private Set&lt;Child&gt; childs;
  
      //setter and getters.
  }
  </code></pre>
  
  <p><strong>Child Class.</strong></p>
  
  <pre><code>@Entity
  @Table (name = "child")
  public class Report {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column (name = "id")
      private int id;
      @Column (name = "name")
      private String name;
  
      @ManyToOne
      @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,org.hibernate.annotations.CascadeType.MERGE})
      @JoinColumn(name="parent_id")
      private Parent defaultchild;    
  
      @ManyToMany(fetch = FetchType.LAZY)
      @JoinTable(name = "group",joinColumns={@JoinColumn(name="fk_child_id")},inverseJoinColumns={@JoinColumn(name="fk_group_id")})
      private Set&lt;XXX&gt; groups = new HashSet&lt;XXX&gt;(0);
      //Setter and Getter methods
  }
  </code></pre>
  
  <p><strong>Service Class</strong></p>
  
  <pre><code>public UIGroup getParentByName(String name) {
      return DAO.getParentByName(name);
  }
  
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  public boolean updateParent(Parent parent) {
      return DAO.updateParent(parent);
  }
  
  
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  public boolean deleteParent(Parent parent) {
      return DAO.deleteParent(parent);
  }
  </code></pre>
  
  <p><strong>DAO Class.</strong></p>
  
  <pre><code>public UIGroup getParentByName(String name) {
      Query query;
      Parent parent = null;
      try {
          String queryString = " from Parent where name = :name";
          query = sessionFactory.getCurrentSession().createQuery(queryString);
          query.setParameter("name", name);
          uiGroup = (Parent) query.uniqueResult();
      } catch (Exception e) {
          logger.error(e);
      }
      return parent;
  }
  
  public boolean updateParent(Parent parent) {
      boolean result = true;
      Session session = null;
      Transaction tx = null;
      try {
          session = sessionFactory.getCurrentSession();
          tx = session.beginTransaction();
          session.merge(parent);
          tx.commit();
          session.flush();
      } catch (HibernateException e) {
          result = false;
          logger.error(e);
      }// end of try-catch block.
      return result;
  }
  
  
  public boolean deleteParent(Parent parent) {
      boolean result = true;
      Session session = null;
      try {
          session = sessionFactory.getCurrentSession();
          session.delete(parent);
      } catch (HibernateException e) {
          result = false;
          logger.error( + e);
      }
      return result;
  }
  </code></pre>
  
  <p>but when I am trying to invoke following code </p>
  
  <pre><code>Parent otherParent = Service.getParentByName("Other");
  Parent parent = Service.getParentByName("XYZ");
  //here I am assigning childs assign to XYX parent to other Parent
  Set&lt;Child&gt; childs = new TreeSet&lt;Child&gt;(Child.COMPARE_BY_ID);
  childs.addAll(otherParent.getchildes());
  childs.addAll(parent.getchilde());
  otherParent.setChilds(childs);
  Service.updateParent(otherParent);
  Service.deleteParent(parent);
  </code></pre>
  
  <p>I am getting following error.</p>
  
  <p><em><strong>Error</em></strong></p>
  
  <pre><code>java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`database`.`child`, CONSTRAINT `fk_parent_child` FOREIGN KEY (`parent_id`) REFERENCES `child` (`id_parent`))
  </code></pre>
  
  <p>which means my update code is not working properly following is log of Service.updateParent(otherParent) statement</p>
  
  <pre><code>SELECT parent0_.id_parent AS id1_135_1_, parent0_.name AS name135_1_, childs1_.parent_id AS parent4_3_, childs1_.id AS id3_, childs1_.id AS id143_0_, childs1_.child_name AS child2_143_0_, childs1_.is_sea_child AS is3_143_0_, childs1_.parent_id AS parent4_143_0_ 
  FROM ui_parent parent0_ 
  LEFT OUTER JOIN child childs1_ ON parent0_.id_ui_parent=childs1_.parent_id 
  WHERE parent0_.id_parent=1
  </code></pre>
  
  <p>please help me I don't know what went wrong with this code 
  thanks in advance.</p>