title: Deleting an element from one-to-many collection (Java + HIbernate + Struts)
body: <p>I can't delete a child object from the database.  From the <code>org.apache.struts.action.Action.execute()</code> method, I am removing the child from the parent's <code>List</code>, and also calling <code>session.delete(child)</code>.  I've simplified the code below and only included what I believe to be relavent.</p>
  
  <p><br /></p>
  
  <h2>Hibernate Mapping</h2>
  
  <pre><code>&lt;class 
      name="xxx.xxx.hibernate.Parent" 
      table="parent"&gt;
  
      ...
  
      &lt;list
          name="children"
          cascade="all,delete-orphan"
          lazy="true"
          inverse="true"&gt;
  
          &lt;key column="parent_id"/&gt;
          &lt;index column="list_index"/&gt;
          &lt;one-to-many class="xxx.xxx.hibernate.Child"/&gt;
      &lt;/list&gt;
  &lt;/class&gt;
  
  &lt;class 
      name="xxx.xxx.hibernate.Child" 
      table="child"&gt;
  
      ...
  
      &lt;many-to-one
          name="parent"
          class="xxx.xxx.hibernate.Parent"
          not-null="true"
          column="parent_id" /&gt;
  
  &lt;/class&gt;
  </code></pre>
  
  <p><br /></p>
  
  <h2>Excerpt from execute() method</h2>
  
  <pre><code>Transaction tx = session.beginTransaction();  //session is of type org.hibernate.Session
  
  try {
      Parent parent = (Parent) session.get(Parent.class, getParentId());
  
      Iterator i = form.getDeleteItems().iterator();  //form is of type org.apache.struts.action.ActionForm
      while(i.hasNext()){
          Child child = (Child) i.next();
          session.delete(child);
          parent.getChildren().remove(child); //getChildren() returns type java.util.List
      }
  
      session.saveOrUpdate(parent);
      tx.commit();
  } ...
  </code></pre>
  
  <p><br />
  I've tried with only <code>session.delete(child);</code> and I've tried with only <code>parent.getChildren().remove(child);</code> and with both lines, all without success.  There are no errors or thrown exceptions or anything of the sort.  I'm sure this code gets called (I've even used <code>System.out.println();</code> to trace what's happening), but the database isn't updated.    I can add children using similar code, edit non-collection properties of existing children, edit the parent's properties, all of that works, just not deleting!</p>
  
  <p>According to the <a href="https://www.hibernate.org/116.html#A18" rel="nofollow noreferrer">Hibernate FAQ</a> I'm doing the mapping right, and according to <a href="https://stackoverflow.com/questions/1289033/deleting-thing-a-in-hibernate-does-not-update-objects-with-collections-of-a">this SO question</a> I've got the right logic.  I've looked all over the internet and can't seem to find anything else.</p>
  
  <p>What am I doing wrong?  Please help!  Thanks.</p>
  
  <p><br /></p>
  
  <h2>Notes on versions</h2>
  
  <p>Everything is a few years old:</p>
  
  <ul>
  <li>Java 1.4.2</li>
  <li>SQL Server 2005</li>
  <li>Hibernate 3.0.5</li>
  <li>Struts 1.2.7</li>
  <li>Apache Tomcat 5.0.28</li>
  </ul>