Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

has one relationships and deletion in grails

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?

like image 633
prabhap Avatar asked Jul 28 '10 07:07

prabhap


1 Answers

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.

like image 114
Daniel Engmann Avatar answered Nov 17 '22 18:11

Daniel Engmann