title: GORM abstract domain class
body: <p>I have 2 abstract Grails domain classes that look like</p>
  
  <pre><code>abstract class A {
     static hasMany = [ b : B ]
     static mapping = {
       tablePerHierarchy false
     }
  }
  
  class AChild extends A {
  }
  
  abstract class B {
     static belongsTo = A
     static mapping = {
       tablePerHierarchy false
     }
  }
  
  class BChild extends B {
  }
  </code></pre>
  
  <p>I have some code that does</p>
  
  <pre><code>A a = new AChild()
  a.b &lt;&lt; new BChild()
  a.save(flush:true)
  </code></pre>
  
  <p>This works as expected</p>
  
  <p>Now, when I try to do</p>
  
  <pre><code>b.delete(flush:true)
  </code></pre>
  
  <p>This fails because of an referential integrity violation in the join table created between A/B. It looks like the first query by GORM as part of the delete is to do a delete from the <code>B</code> table, not the join table, and the join has a reference back to the <code>B</code> table by its id, which causes the violation.</p>
  
  <pre><code>delete from b where id=? and version=?
  </code></pre>
  
  <p>The reason I would like to keep <code>A</code> and <code>B</code> in the domain folder is so I can do things like <code>A.list()</code>, so moving it out to <code>src/groovy</code> would only be a very last option.</p>