在grails中添加另一个child实例的最佳实践是什么?
如果我有这个域名
class Parent {
String name
static hasMany = [children: Child]
}
class Child {
String name
static belongsTo = [parent: Parent]
}
如果要创建新的子代,是否要手动获取父代的ID并将其存储到子代实例中?
最佳答案
试试这个:
Parent parent = Parent.get(parentId)
Child child = new Child(
parent: parent,
name: 'name'
)
child.save()
parent.addToChildren(child)
parent.save()
要么...
Parent parent = Parent.get(parentId)
Child child = new Child()
child.name = 'name'
child.parent = parent
child.save()
parent.addToChildren(child)
parent.save()
关于grails - 在Grails中添加新的子实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33735835/