grails 2.5.1 remove从一对多给出奇怪的行为(不是删除)(grails 2.5.1 removeFrom one

编程入门 行业动态 更新时间:2024-10-27 11:27:32
grails 2.5.1 remove从一对多给出奇怪的行为(不是删除)(grails 2.5.1 removeFrom one-to-many giving strange behavior (not removing))

我很确定我做错了什么,因为这显然有效。 简化课程:

class Person { String name static hasMany = [cats:Cat] } class Cat { String name Person person static belongsTo = Person static constraints = { person(nullable:false) } String toString() { "${person.name}-${name}" } }

简单的东西,一个人有很多猫,猫必须只属​​于一个人。

现在,当我在Service类中执行以下操作时,我得到了一些奇怪的结果:

delete(Cat cat) { Person owner = cat.person log.debug("Cats before removing ${cat} (id=${cat.id}): ${owner.cats} -- ${owner.cats*.id}") owner.removeFromCats(cat); log.debug("Removed from owner ${owner}, owner now has ${owner.cats} -- ${owner.cats*.id}") log.debug("Cat to delete is now: ${cat} and belongs to... ${cat.person}") cat.delete(flush:true) }

错误是“对象将被重新保存,等等等等”

org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)

这个奇怪的位是调试结果,当被调用来移除由“Bob”拥有的猫“蓬松”时:

Cats before removing Bob-Fluffy (id=1356): [Bob-Fluffy] -- [1356] Removed from owner Bob, owner now has [null-Fluffy] -- [1356] Cat to delete is now: null-Fluffy and belongs to... null

这是怎么回事“removeFrom”实际上并没有从集合中移除对象? 我清理并重新编译。 对于为什么我不能删除这个对象,我几乎不知所措。

I'm pretty much certain I'm doing something wrong since this obviously works. Simplified classes:

class Person { String name static hasMany = [cats:Cat] } class Cat { String name Person person static belongsTo = Person static constraints = { person(nullable:false) } String toString() { "${person.name}-${name}" } }

Simple stuff, a person has many cats, cats must belong to only a single person.

Now when I do the following in a Service class, I get strange results:

delete(Cat cat) { Person owner = cat.person log.debug("Cats before removing ${cat} (id=${cat.id}): ${owner.cats} -- ${owner.cats*.id}") owner.removeFromCats(cat); log.debug("Removed from owner ${owner}, owner now has ${owner.cats} -- ${owner.cats*.id}") log.debug("Cat to delete is now: ${cat} and belongs to... ${cat.person}") cat.delete(flush:true) }

And the error is "object would be resaved, blah blah"

org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)

The weird bit is the debug results, when called to remove cat "Fluffy" who's owned by "Bob":

Cats before removing Bob-Fluffy (id=1356): [Bob-Fluffy] -- [1356] Removed from owner Bob, owner now has [null-Fluffy] -- [1356] Cat to delete is now: null-Fluffy and belongs to... null

What's going on that "removeFrom" isn't actually removing the object from the collection? I cleaned and recompiled. Pretty much at a loss as to why I can't delete this object.

最满意答案

我会尝试将字段人员删除为人员,并仅保留像这样的belongsTo字段

class Cat { String name static belongsTo = [person:Person] static constraints = { person(nullable:false) } String toString() { "${person.name}-${name}" } }

It looks like what was happening in my case is that cat.person has getting stale somehow, even though it's the first thing in the method. Calling cat.refresh() didn't work, but calling owner.refresh() after extracting it from the cat.

更多推荐

本文发布于:2023-07-17 04:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多给   奇怪   grails   removeFrom   remove

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!