title: Grails many-to-many cascade saving in a loop
body: <p>I have a many-to-many relation between class A and B, A owns the relation. <strong>so A has responsibility to cascade save B and to update join table .</strong></p>
  
  <p>pseudo code : </p>
  
  <pre><code>A {
       hasMany = [bCollection : B]
  }
  
  B {
       hasMany = [aCollection : A]
       belongsTo = A
  }
  </code></pre>
  
  <p>I have a loop for batch insert</p>
  
  <pre><code>        // transaction begin and hibernate session opened
              for(int i in 1..10){
                 b = new B(...)                       
                 a.addToBCollection(b)
                 a.save(flush:true)
                 ...
              }
     //transaction committed
  </code></pre>
  
  <p>All ten b instances were saved with cascade saving from A to B, each b instance has a generated id (1001 ~ 1011) </p>
  
  <pre><code> table b
  
      1001 b1
  
      1002 b2
  
      1003 b3
  
      ...
  </code></pre>
  
  <p>what is strange is that  there is only one record in join table and it's the last b instance which is taken account, which means in join table i have one record :</p>
  
  <pre><code>join table a_b
  
  (a.id,1011)
  </code></pre>
  
  <p>have you any idea ?</p>
  
  <p>thank you : )</p>