我在向父母添加子域时遇到了一些困难。这是课程:
class parent{
String firstName
String lastName
String dobYear
String dobMonth
String dobDay
Date dateCreated
Date lastUpdated
long version
static hasMany = [
chidlren: Children
]
static mapping = {
cache true
id generator: 'assigned'
columns {
firstName type:'text'
lastName type:'text'
dobYear type:'text'
dobMonth type:'text'
dobDay type:'text'
}
}
static constraints = {
firstName (nullable:true)
lastName (nullable:true)
dobYear (nullable:true)
dobMonth (nullable:true)
dobDay (nullable:true)
id (nullable:false)
}
}
孩子们:
class Children{
String skillId
String skillName
String skillProficiency
String skillYears
Date dateCreated
Date lastUpdated
long version
static belongsTo = [parent:Parent]
static mapping = {
cache true
columns {
skillId type:'text'
skillName type:'text'
skillProficiency type:'text'
skillYears type:'text'
}
}
static constraints = {
skillId (nullable:true)
skillName (nullable:true)
skillProficiency (nullable:true)
skillYears (nullable:true)
}
}
然后我像这样实例化父类: //my xml def feed= new XmlSlurper().parseText(linkedinResponse);
def newParent= new Parent(
firstName:"${feed.'first-name'}",
lastName:"${feed.'last-name'}",
dobYear :"${feed.'date-of-birth'.'year'}",
dobMonth:"${feed.'date-of-birth'.'month'}",
dobDay :"${feed.'date-of-birth'.'day'}"
)
.id="${feed.id}".toString()
在我的 xml 提要中,我有多个子节点,所以我想创建多个域节点并将它们添加到父节点。xml 的东西工作正常:
feed.skills.skill.each{ mySkill ->
def newChild = new Children(
skillId: mySkill.'id',
skillName: mySkill.'name',
skillProficiency: mySkill.'proficiency',
skillYears : mySkill.'years'
)
newParent.addToChildren(newChild )
}
当我尝试添加孩子时,出现错误(方法签名:com.myapp.Children.call() 适用于参数类型:() 值:[])
除了额外的字段,这与我在 grails 网站上找到的示例有何不同,如下所示:
def parent = new Parent(name:'Dad')
parent.addToChildren(new Child(name:'son'))
parent.addToChildren([name:'daughter'])
谢谢你的帮助
杰森