我在向父母添加子域名时遇到一些困难。这是课程:
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)
}
}
然后我像这样实例化父类: //我的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 feed中,我有多个子节点,所以我想创建多个域节点并将它们添加到父节点。 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 )
}
当我尝试添加子项时,我得到一个错误(o方法的签名:com.myapp.Children.call()适用于参数类型:()值:[])
除了额外字段之外,这与我在grails网站上找到的示例有什么不同,如下所示:
def parent = new Parent(name:'Dad')
parent.addToChildren(new Child(name:'son'))
parent.addToChildren([name:'daughter'])
感谢您的帮助
杰森