Lmichasz wrote:
I have collection mapped with cascade = "all-delete-orphan".
I remove some object from this collection.
Then I create another one with the same primary key as the one removed and add this object to collection.
When I commit this transaction NonUniqueObjectException is thrown.
With this cascade option Hibernate keeps tracking changes to collection and to instances of objects loaded in the same transaction until transaction gets commited and session closed. Your previously loaded instance after deletion from collection was marked as deleted and will be deleted from DB if you save parent and commit transaction. Your new object will be as a new for Hibernate. Hibernate not smart enough to determine that actually you are trying to execute update (and/or delete and insert) Reuse your instance if your are not going to delete it. Or, create a new one with different ID.