-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: saveOrUpdateCopy to solve NonUniqueObjectException
PostPosted: Wed Oct 05, 2005 2:10 am 
Newbie

Joined: Tue Jun 28, 2005 11:02 am
Posts: 2
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.