gpt4 book ai didi

grails - 在Grails中添加新的子实例

转载 作者:行者123 更新时间:2023-12-02 15:22:04 26 4
gpt4 key购买 nike

在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/

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