Hibernate version: 2.1.4
A question related to updating a collection:
According to the manual, this is the order of how SQL is executed:
Quote:
1. all entity insertions, in the same order the corresponding objects were saved using Session.save()
2. all entity updates
3. all collection deletions
4. all collection element deletions, updates and insertions
5. all collection insertions
6. all entity deletions, in the same order the corresponding objects were deleted using Session.delete()
My setting (the following are pseudocode):
I have a persister Object A that have a collection c
In my code I do the following in order:
1. A.c.clear()
2. A.c.add(object)
3. update(A)
That is, I clear the collection and then adds an object to the collection. I have set the necessary cascading attributes on A.c (I've tried both "all-delete-orpans" and "all").
The order of SQL execution that I intuitively expected was that the DELETE statements to be executed first, then the INSERTs. Instead, as the aforementioned specifies, the INSERTs are being executed _before_ the DELETEs.
The problem with this is that I'm trying to insert objects that already existed before the DELETE, and when the INSERT is executed before the DELETE, this causes a PK constraint violation.
A second point, the database triggers some functions when an INSERT is done, so I really want to insert elements instead of updating these.
I have also tested dereferencing the collection, initialising a new collection and setting this, as described in chapter 14.1.4. However, this doesn't work with "cascade=all-delete-orphan", and the elements in the collection thus does not get deleted, causing a constraint violation when I try to insert elements with the same PK that already resides in the "old" collection.
I guess my question boils down to:
Is there a way to alter the order of how the SQLs are executed (in the same session)?
As of now, my options, as far as i can see, are:
- Forcing the DELETEs and INSERT in different sessions.
- Redesign the database triggers, and check the collection for object existence before updating the collection. (Not preferrable)
Thanks for any comments, views, etc...
Regards, Jen