-->
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.  [ 3 posts ] 
Author Message
 Post subject: saveOrUpdateCopy problem
PostPosted: Wed Nov 03, 2004 9:44 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 6:24 pm
Posts: 45
Hibernate version: 2.1.6

Hi Folks - this is in relation to the issue of choosing between using session.saveOrUpdate() and session.saveOrUpdateCopy().

Skip to the bottom for my implemented workaround - its very hacky, but I don't see a better way to handle the situation.

We have a system that operates fairly simply by allowing code to
- receive a modified object from our view layer code
- call a utility class method to save/update the object

It works fine to call session.saveOrUpdate() with an object when another version of the object has not been loaded.

However, in many cases, we need to examine the previously existing version of the same object before updating it.

Fine, so we session.load() the old version, check stuff, then use saveOrUpdateCopy() to avoid a NonUniqueObjectException.

The problem is that now the code that performs the saveOrUpdate() or saveOrUpdateCopy() has to be aware of the context of the surrounding code so that it knows whether the object was previously loaded.

So, to keep the coupling down between these layers of code, I would like our persistence utility to be able to expose a single method that will do the right thing.

The best I have come up with is this:

Code:
                //determine if there is a loaded version of the object
                boolean entityLoaded = false;
                ClassPersister cp = ((SessionImplementor)session).getPersister(object);
                Serializable id = cp.getIdentifier(object);
                Key objectKey = new Key(id, cp);
                entityLoaded = ((SessionImplementor)session).getEntity(objectKey) != null;
                if (entityLoaded){
                    if (log.isDebugEnabled()) {
                        log.debug("found loaded entity for object: "+ object);
                    }
                    session.saveOrUpdateCopy(object);
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("did NOT find loaded entity for object: "+ object);
                    }
                    session.saveOrUpdate(object);
                }


I would really rather not use the hibernate mechanisms of SessionImplementor, ClassPersister and Key, but I don't think the hibernate session has an exposed method for indicating that "i know the object may already be loaded, in that case please use saveOrUpdateCopy() pattern".

Any thoughts on this?

thanks
tyson


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 10:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Would not Session.contains() do just what you are doing here?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 04, 2004 4:01 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 6:24 pm
Posts: 45
michael wrote:
Would not Session.contains() do just what you are doing here?


No, not without imposing restrictions on my objects' equals and hashcode (i.e. if my equals/hashcode only delt with object's id I assume it would work fine), since session.contains does:

Code:
return entityEntries.containsKey(object);


I would like a method like: session.containsObjectWithSameId(object) so that I can know ahead of time if this situation would potentially cause a NonUniqueObjectException, so that I can avoid it by using saveOrUpdateCopy().


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

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.