dimanche 18 avril 2010

Hibernate: moving objects from one parent to another

Using Hibernate, I usually default collection "cascade" attribute to "all-delete-orphan". The allows me to not care about cascading at all, and let Hibernate do everything.

However, if you want to move an object from one parent to another (in my case, say a peep is removed from a FarmTile and added to a BoardTile or a Card), you will get an exception like "deleted object would be re-saved by cascade (remove deleted object from associations".

To solve this, simply change the 'all-delete-orphan" to "all" - which means you will need to manually delete orphans if you need them deleted though.