开发者

GORM 1:N Association Cascades Delete Without belongsTo!

开发者 https://www.devze.com 2023-01-16 09:06 出处:网络
I would 开发者_如何转开发like to create a one-to-many association that does not cascade deletes. Reading the Grails Reference it says

I would 开发者_如何转开发like to create a one-to-many association that does not cascade deletes. Reading the Grails Reference it says

The default cascading behaviour is to cascade saves and updates, but not deletes unless a belongsTo is also specified

That isn't the behavior I'm seeing. With the following class implementations I get cascaded updates, saves, and deletes without any belongsTo:

class A {
   static hasMany = [bees: B]
}
class B { }

In one Hibernate session I do the following to verify cascade updates work:

def a = new A()
a.save()
def b1 = new B()
a.addToBees(b1)
def b2 = new B()
a.addToBees(b2)

Then, in another Hibernate session, the following code deletes all instances of A and B:

A.list().each { a -> a.delete() }

Is this a bug? Is the documentation wrong? Am I doing something incorrectly? I'm using the Grails console to verify this behavior.


Are you sure that b1 and b2 are committed to the database? I'd have thought a.save at end, rather than on second line might yield the results you were expecting.


Once you save the parent object, the dynamic "addTo" methods automatically save what you add.

The problem actually turned out to be the Grails Console. Once I threw the code in an integration test it worked just fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消