Hi,
I've got kind of a dilemma here with the SAVE_UPDATE cascade style in annotations. I do have an abstractmedia object that has a ManyToOne relation to a creator object and it seems that using SAVE_UPDATE is the only way of letting Hibernate persist new creator objects when updating the abstractmedia object. So the abstractmedia object is only updated, but before that the creator object needs to be persisted. This only happens when I specify SAVE_UPDATE (I'm using the saveOrUpdate method for the abstractmedia object), when I leave it out I get an except that I need to persist the creator first.
Now the dilemma is that when I change the creator to another one and use saveOrUpdate that new creator object is already associated to the session and thus the update on the creator fails with a NonUniqueObjectException (because saveOrUpdate cascade's to the cached creator).
I currently can't see a way to find out wether a given object is associated to a session and thus there's no way for me to determine wether the creator object attached to an abstractmedia object needs to be persisted or not. The code that loads existing creator's from the database to attach them to the abstractmedia object is completely detached from the code that saves or updates an abstractmedia object.
Anybody understood that and has an idea how to fix that (without having to do something like if creator.id == 0 -> persist)?
|