gpt4 book ai didi

grails - 在 grails 中有一个关系和删除

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

我应该如何删除 grails 中 hasOne 关系中的子对象
例如:

class Face {
static hasOne = [nose: Nose]
}
class Nose {
Face face
static belongsTo= Face
}

我尝试通过两种方式删除子对象
1. face.nose.delete()
2. nose.delete()

我总是以两种方式通过级联重新保存相同的异常已删除对象。还有一个我是否有任何用于 hasOne 的动态方法(例如 addTo 和 removeFrom 的 hasMany)?
有什么帮助吗?

最佳答案

你可以试试

face.nose = null
face.save()
nose.delete()

如果您只删除 Nose ,则仍设置属性 face.nose。稍后调用 face.save() 将重新保存 Nose 。

如果您只设置 face.nose = null(不保存),则更改不会保存到数据库中。稍后对数据库的查询以获取人脸将为您提供带有 Nose 的人脸,然后 save() 将重新保存它。

关于grails - 在 grails 中有一个关系和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3350652/

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