title: Grails - when saving domain objects - i am getting "rejected value [null] error"
body: <p>I have three domain objects defined as :</p>
  
  <pre><code>class Member {
      String name
  
      static constraints = {}
      static belongsTo=[community:Community]
  }
  
  class Community {
      String leaderName
      String code
  
      static constraints = {}
      static hasMany=[members: Member]
      static belongsTo=[bank:Bank]
  }
  
  class Bank {
  
      String bankName
      static hasMany=[communities: Community]
      static constraints = {}
  }
  </code></pre>
  
  <p>When I tried to initialize these domain objects with some test data in <code>BootStrap.groovy</code> config class as :</p>
  
  <pre><code> def init = { servletContext -&gt;
  
              def m1 = new Member(name:"M1_Name")
              def m2 = new Member(name:"M2_Name")
              def m3 = new Member(name:"M3_Name")
  
              m1.save(failOnError:true)
              m2.save(failOnError:true)
              m3.save(failOnError:true)
  
              def comA = new Community(leaderName:"LeaderA", code:"AA")
              def comB = new Community(leaderName:"LeaderB", code:"BB")
  
              comA.addToMembers(m1)
              comA.addToMembers(m2)
              comB.addToMembers(m3)
  
              comA.save(failOnError:true)
              comB.save(failOnError:true)
  
              def bankA = new Bank(bankName:"BankA")
              def bankB = new Bank(bankName:"BankB")
  
              bankA.addToCommunities(comA)
              bankB.addToCommunities(comB)
  
              bankA.save(failOnError:true)
              bankB.save(failOnError:true)
          }
  </code></pre>
  
  <p>I am getting following error :</p>
  
  <pre><code>| Loading Grails 2.0.4
  | Configuring classpath.
  | Environment set to development.....
  | Packaging Grails application.....
  | Compiling 3 source files.....
  | Running Grails application
  | Error 2012-07-13 22:14:44,798 [pool-5-thread-1] ERROR context.GrailsContextLoader  - Error executing bootstraps: Validation Error(s) occurred during save():
  - Field error in object 'mygrailtests.Member' on field 'community': rejected value [null]; codes [mygrailtests.Member.community.nullable.error.mygrailtests.Member.community,mygrailtests.Member.community.nullable.error.community,mygrailtests.Member.community.nullable.error.mygrailtests.Community,mygrailtests.Member.community.nullable.error,member.community.nullable.error.mygrailtests.Member.community,member.community.nullable.error.community,member.community.nullable.error.mygrailtests.Community,member.community.nullable.error,mygrailtests.Member.community.nullable.mygrailtests.Member.community,mygrailtests.Member.community.nullable.community,mygrailtests.Member.community.nullable.mygrailtests.Community,mygrailtests.Member.community.nullable,member.community.nullable.mygrailtests.Member.community,member.community.nullable.community,member.community.nullable.mygrailtests.Community,member.community.nullable,nullable.mygrailtests.Member.community,nullable.community,nullable.mygrailtests.Community,nullable]; arguments [community,class mygrailtests.Member]; default message [Property [{0}] of class [{1}] cannot be null]
  
  Message: Validation Error(s) occurred during save():
  - Field error in object 'mygrailtests.Member' on field 'community': rejected value [null]; codes [mygrailtests.Member.community.nullable.error.mygrailtests.Member.community,mygrailtests.Member.community.nullable.error.community,mygrailtests.Member.community.nullable.error.mygrailtests.Community,mygrailtests.Member.community.nullable.error,member.community.nullable.error.mygrailtests.Member.community,member.community.nullable.error.community,member.community.nullable.error.mygrailtests.Community,member.community.nullable.error,mygrailtests.Member.community.nullable.mygrailtests.Member.community,mygrailtests.Member.community.nullable.community,mygrailtests.Member.community.nullable.mygrailtests.Community,mygrailtests.Member.community.nullable,member.community.nullable.mygrailtests.Member.community,member.community.nullable.community,member.community.nullable.mygrailtests.Community,member.community.nullable,nullable.mygrailtests.Member.community,nullable.community,nullable.mygrailtests.Community,nullable]; arguments [community,class mygrailtests.Member]; default message [Property [{0}] of class [{1}] cannot be null]
  
     Line | Method
  -&gt;&gt;  13 | doCall                           in BootStrap$_closure1
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   301 | evaluateEnvironmentSpecificBlock in grails.util.Environment
  |   294 | executeForEnvironment . . . . .  in     ''
  |   270 | executeForCurrentEnvironment     in     ''
  |   303 | innerRun . . . . . . . . . . . . in java.util.concurrent.FutureTask$Sync
  |   138 | run                              in java.util.concurrent.FutureTask
  |   886 | runTask . . . . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run                              in     ''
  ^   662 | run . . . . . . . . . . . . . .  in java.lang.Thread
  </code></pre>
  
  <p>Here, line 13 is </p>
  
  <pre><code>m1.save(failOnError:true)
  </code></pre>
  
  <p>Could you please suggest me what's wrong going on? Or what I am missing. </p>
  
  <p><strong>UPDATE :</strong>
  It is working without errors when I remove <code>belongsTo</code> relation in <code>Member</code> and <code>Community</code> objects.</p>