title: Groovy removeAll closure not removing objects in Set
body: <p>I'm using data binding with parent/child relationships in Grails 2.3.7 and am having trouble with deletes.  The form has many optional children, and to keep the database tidy I'd like to purge blank (null) values.  I've found some nice articles which suggest using removeAll to filter my entries but I can't get remove or removeAll to work!</p>
  
  <p>For example... (Parent has 10 children, 5 are blank)</p>
  
  <pre><code>def update(Parent parent) {
    parent.children.getClass() // returns org.hibernate.collection.PersistentSet
    parent.children.size() // returns 10
    parent.children.findAll{ it.value == null }.size() // returns 5
    parent.children.removeAll{ it.value == null } // returns TRUE
    parent.children.size() // Still returns 10!!!
  }
  </code></pre>
  
  <p>I've read PersistentSet is finicky about equals() and hashCode() being implemented manually, which I've done in every domain class.  What baffles me is how removeAll can return true, indicating the Collection has changed, yet it hasn't.  I've been stuck on this for a couple days now so any tips would be appreciated.  Thanks.</p>
  
  <p>Update:</p>
  
  <p>I've been experimenting with the Child hashcode and that seems to be the culprit.  If I make a bare-bones hashcode based on the id (bad practice) then removeAll works, but if I include the value it stops working again.  For example...</p>
  
  <pre><code>// Sample 1: Works with removeAll
  int hashCode() {
    int hash1 = id.hashCode()
    return hash1
  }
  
  // Sample 2: Doesn't work with removeAll
  int hashCode() {
    int hash1 = id.hashCode()
    int hash2 = value == null ? 0 : value.hashCode()
    return hash1 + hash2
  }
  
  // Sample Domain classes (thanks Burt)
  class Parent {
    static hasMany = [children: Child]
  }
  
  class Child {
    String name
    String value
    static constraints = {
      value nullable: true
    }
  }
  </code></pre>
  
  <p>This behavior is explained by the data binding step updating data, making it dirty.  (ie: child.value.isDirty() == true)  Here's how I understand it.</p>
  
  <p>First Grails data binding fetches the Parent and children, and the hashcode of each Child is calculated.  Next, data updates are applied which makes child.value dirty (if it changed) but the Set's hashcodes remain unchanged.  When removeAll finds a match it builds a hashCode with the dirty data, but that hashcode is NOT found in the Set so it can't remove it.  Essentially removeAll will only work if ALL of my hashCode variables are clean.</p>
  
  <p>So if the data must be clean to remove it, one solution is to save it twice. Like this...</p>
  
  <pre><code>// Parent Controller
  def update(Parent parent) {
    parent.children.removeAll{ it.value == null } // Removes CLEAN children with no value
    parent.save(flush:true) 
    parent.refresh() // parent.children is now clean
    parent.children.removeAll{ it.value == null } // Removes (formerly dirty) children
    parent.save(flush:true) // Success!
  }
  </code></pre>
  
  <p>This works though it's not ideal. First I must allow null values in the database, though they only exist briefly and I don't want them. And second it's kinda inefficient to do two saves. Surely there must be a better way?</p>