views:

60

answers:

2

How should i delete the child object in a hasOne relationship in grails for eg:

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?Thanks

A: 

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()
MTH
I think the Face should not be deleted only the Nose.
Daniel Engmann
A: 

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.

Daniel Engmann