Java Grails-在保存域对象时,我得到;拒绝的值[null]错误“;

Java Grails-在保存域对象时,我得到;拒绝的值[null]错误“;,java,grails,orm,gorm,Java,Grails,Orm,Gorm,我有三个定义为的域对象: class Member { String name static constraints = {} static belongsTo=[community:Community] } class Community { String leaderName String code static constraints = {} static hasMany=[members: Member] stati

我有三个定义为的域对象:

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 = {}
}
当我试图用
BootStrap.groovy
config类中的一些测试数据初始化这些域对象时,如下所示:

 def init = { servletContext ->

            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)
        }
我遇到以下错误:

| 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
->>  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
这里是第13行

m1.save(failOnError:true)
你能告诉我发生了什么事吗?或者我错过了什么

更新:
当我删除
成员
社区
对象中的
归属关系时,它工作正常。

当您将
归属:社区
部分添加到
成员
时,您是说
成员
将属于一个
社区

构造
成员
对象时,您没有提供与
成员
连接的
社区
。像这样提交
成员
会爆炸,因为您还没有说出它所属的
社区


您不需要在那里调用
save
,因为
belongsTo
标记
社区将负责保存。取出3 m[x].save()行,将下面的
读到
代码,看看它是否按预期工作。

这是对象创建的顺序。belongsTo将创建一个外键关系(至少在默认情况下),因此您必须按照示例中显示的相反顺序创建对象(例如银行、社区、成员)


您还可以更深入地了解约束和映射,以更改某些默认机制。Grails指南相当长,有时我发现它假设了一些以前关于Hibernate的知识,但它应该包含了大部分内容(您可能需要转到“其他”和“高级”主题)。

注意:由于
Bank
具有相同的关系,请删除
com.save()
行,并让
bank.save()
代码将其全部提交。感谢您的解释,只需保留
bankA.save(failOnError:true)和bankB.save(failOnError:true)
最后解决了问题。