gpt4 book ai didi

java - Grails - 保存域对象时 - 我得到 "rejected value [null] error"

转载 作者:行者123 更新时间:2023-11-30 06:29:30 25 4
gpt4 key购买 nike

我定义了三个域对象:

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 配置类中的一些测试数据初始化这些域对象时:

 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)

你能告诉我发生了什么问题吗?或者我错过了什么。

更新:当我删除 MemberCommunity 对象中的 belongsTo 关系时,它工作正常。

最佳答案

当您将 belongsTo: Community 部分添加到 Member 时,您表示 Member 将属于一个 Community

当您构建Member 对象时,您并未提供与Member 相关联的Community。像这样提交 Member 会爆炸,因为您还没有说出它所属的 Community

无论如何您都不需要那些save 调用,因为belongsTo 标记Community 将负责保存。取出 3 m[x].save() 行,读取 belongsTo 代码,看看它是否按预期工作。

关于java - Grails - 保存域对象时 - 我得到 "rejected value [null] error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11474653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com