title: Hibernate how to remove item from list
body: <p>I have been facing some challenges of understanding how list are managed in hibernate.</p>
  
  <p>I have looked at the following post:  <a href="https://stackoverflow.com/questions/549961/hibernate-removing-item-from-a-list-does-not-persist">hibernate removing item from a list does not persist</a> but that did not help.</p>
  
  <p>So here is the parent:</p>
  
  <pre><code>public class Material extends MappedModel implements Serializable
  {
      /**
       * List of material attributes associated with the given material
       */
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
      @JoinColumn(name = "material_id", nullable = false)
      @org.hibernate.annotations.IndexColumn(name = "seq_number", base = 0)
      private List&lt;MaterialAttribute&gt; materialAttributes = new ArrayList&lt;MaterialAttribute&gt;();
  </code></pre>
  
  <p>here is the child</p>
  
  <p>public class MaterialAttribute extends MappedModel implements Serializable</p>
  
  <pre><code>{
      /**
       * identifies the material that these attributes are associated with
       */
      @ManyToOne
      @JoinColumn(name = "material_id", nullable=false, updatable=false, insertable=false)
      private Material material;
  </code></pre>
  
  <p>So in my Service class I am doing the following:</p>
  
  <pre><code>public void save(MaterialCommand pCmd)
  {
  Material material = new Material();
  if(null != pCmd.getMaterialId())
  {
      material = this.loadMaterial(pCmd.getMaterialId());
  }
  
  material.setName(pCmd.getName());
  material.getMaterialAttributes().clear();
  
      List&lt;MaterialAttribute&gt; attribs = new ArrayList&lt;MaterialAttribute&gt;();
      if(CollectionUtils.isNotEmpty(pCmd.getAttribs()))
      {
          Iterator&lt;MaterialAttributeCommand&gt; iter = pCmd.getAttribs().iterator();
          while(iter.hasNext())
          {
  MaterialAttributeCommand attribCmd = (MaterialAttributeCommand) iter.next();
              if (StringUtils.isNotBlank(attribCmd.getDisplayName()))
  {
          MaterialAttribute attrib = new MaterialAttribute();
          attrib.setDisplayName(attribCmd.getDisplayName());
          attrib.setValidationType(null);
          attribs.add(attrib);
  }
          }
      }
  
      material.setMaterialAttributes(attribs);
      this.getMaterialDao().update(material);
  }
  </code></pre>
  
  <p>So with the above set up the first time everything is created in the database correctly.  The second time, my expectation was that the the first set of items in the collection would of been removed and only the new items would be in the database.  That did not happen.  The original items in the child are there along with the the new items and that that the seq number start at 0 again.</p>
  
  <p>Also, I am seeing the following error </p>
  
  <p>org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:</p>
  
  <p>What am I missing.</p>