From the API documentation:
Quote:
This exception is thrown when an operation would break session-scoped identity. This occurs if the user tries to associate two different instances of the same Java class with a particular identifier, in the scope of a single Session.
It probably has less to do with your mapping and more to do with your usage. Since you didn't post the code between openSession and closeSession, I can only speculate. Here is one possible scenario:
Open session
Begin transaction
Load object with ID 5
Create new object, set its ID to 5
Attempt to save new object, resulting in NonUniqueObjectException
...
Another problem may be that you have two rows in your database with the same value in the column you have mapped to your ID. This might happen if you don't have a UNIQUE constraint on your ID column and the data got messed up somehow (double-save, for example). It could also happen on a table with a composite key, if there was no UNIQUE constraint containing all of the key columns.
- Jesse