在grails 中有一个关系和删除

发布于 2024-09-12 06:03:24 字数 356 浏览 6 评论 0原文

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

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

我尝试通过两种方式删除子对象,

1. face.nose.delete()
2. nose.delete()

但我总是得到相同的异常:通过级联以两种方式重新保存已删除的对象。还有一件事,我是否有针对 hasOne 的动态方法(例如 hasMany 的 addTo 和 removeFrom)? 有什么帮助吗?

How should I delete the child object in a hasOne relationship in grails
for e.g.:

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

I tried deleting the child object by two ways

1. face.nose.delete()
2. nose.delete()

I always get the same exception Deleted object resaved by cascade in both the ways. And one more do I have any dynamic methods (like addTo and removeFrom for hasMany) for hasOne?
Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南冥有猫 2024-09-19 06:03:24

你可以尝试

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

如果你只删除nose,那么属性face.nose仍然被设置。稍后调用face.save()将重新保存鼻子。

如果您仅设置face.nose = null(不保存),则更改不会保存到数据库中。稍后对数据库进行查询以获取人脸时,将为您提供一张带有鼻子设置的人脸,并且 save() 将重新保存它。

You could try

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

If you only delete nose then the property face.nose is still set. A later call of face.save() would resave the nose.

If you only set face.nose = null (without saving) then the change isn't saved to the database. A later query to the database to get a Face would give you a Face with the nose set and a save() would resave it.

夏日浅笑〃 2024-09-19 06:03:24

尝试按如下方式创建您的课程:

class Face {
        Nose nose
}

class Nose {    
        static belongsTo = Face
}

然后删除尝试:

def f = Face.get(1)
f.nose.delete()
f.delete()

Try making your class as follows:

class Face {
        Nose nose
}

class Nose {    
        static belongsTo = Face
}

Then to delete try:

def f = Face.get(1)
f.nose.delete()
f.delete()
霓裳挽歌倾城醉 2024-09-19 06:03:24

试试这个

noseId = face.nose.id
face.nose = null
nose.get(noseId).delete(flush:true)

try this

noseId = face.nose.id
face.nose = null
nose.get(noseId).delete(flush:true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文