开发者

grails / GORM / hibernate - hasMany map not synchronised on save() of parent

Problem deleting associated entities and having this change persisted to the database.

Have a groovy object parent which has many child entities associated with it. When we find this domain object and set this list to null, and call parent.save(flush:true) the child elements remain in the database. Would have expected these to have been deleted. Any suggestions would be great.

class Parent {

static hasMany = [child:Child] ... }

and the child:

class Child {

belongsTo = [Parent] ... }

we add the element and delete:

def child = new Child()

def parent = new Parent(child:child)

parent.save(flush:true) def id = parent.id //from saved entity

/// in开发者_StackOverflow中文版 separate transaction

parent = Parent.get(id) //id from above parent.child = null

parent.save(flush:true)

// check database - child reference still there - expect to have been deleted

Any suggestions about what we've done that wrong would be appreciated. using grails 1.3.5 (latest version).


First thing, you should use child.delete(flush:true) instead of assign null. It's just not approriate. (sorry for the previous mistake)

I recommend you read Peter Ledbrook series: http://blog.springsource.com/2010/07/02/gorm-gotchas-part-2/ . In your case, search "Deleting children"

Update: in case you still not read the above article (specifically useful):

To delete a child, you first need to remove it from the parent collection, then delete it. But it would cause property not null exception since you use belongsTo relation.

Solution: You can add this into Parent class:

static mapping = {
 childs cascade: "all-delete-orphan"
}

The above mapping will help delete all child that don't have a parent. Then you can use

parent.childs.clear()


Start with the documentation.

You need to play with your cascade settings.

Check out

http://www.grails.org/doc/1.0.x/guide/5.%20Object%20Relational%20Mapping%20(GORM).html

section 5.5.2.9


static mapping = 
{
   childs cascade: "all"
}

this works fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜