Hibernate version: 2.1.7
We work with detached objects and use session.saveOrUpdate() to save an object, passed form the UI layer, using a new session.
Now we have the following situation:
1. A detached object Object_A, an instance of Class_A with id 1000, is passed from the UI layer to the Business Logic layer to be saved.
2. Before saving the object, we need to check if the user is authorised. During this check Object_B, an instance of Class_B, is loaded which has a n:1 relation with Class_A. In our case (we don't use lazy loading on class level), this results in the loading of Object_A2 from the database. Object_A2 is an instance of Class_A with id 1000.
3. If we try saveOrUpdate(Object_A) this results in a NonUniqueObjectException, because we have already loaded Object_A2 (which has the same id as Object_A) in the session.
The FAQ suggest to call saveOrUpdate() at the beginning of the transaction, when the session is still empty. But we prefer to check the authorisation first before saving the object instead of saving it first and afterwards doing a rollback when the user appears not to be authorised.
We need to do the authorisation check using the same session, because we have a session-per-thread.
Another option according to the FAQ is to use merge(), but as we have Hibernate 2.1.7 we cannot use merge().
Questions:
- Is saveOrUpdateCopy() the alternative for merge() in Hibernate 2.x?
- Is using saveOrUpdateCopy() the correct solution for the situation described above?
- Why shoudn't you always use saveOrUpdateCopy() instead of saveOrUpdate()? In other words: what's the use of saveOrUpdate() when you have saveOrUpdateCopy()?
I hope somebody can help me with this.