-->
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 conundrum
PostPosted: Mon Nov 01, 2004 9:20 pm 
Beginner
Beginner

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

Hi Folks -
I am working on migrating many EJBs to hibernate entities. Along the way, there are a few spots where we check for differences in the old version and new version of an entity before it is saved - for instance, when checking permissions or auditing the save. In any case, our work flow for a class foo.Bar is something like:

- Session.load old version of foo.Bar
- check user permissions on foo.Bar
- audit the changing fields in foo.Bar
- Session.save the "newer" version of foo.Bar

I am trying to use a single session for the entire sequence.

As you know, this will generate a NonUniqueObjectException.

So, I could use Session.saveOrUpdateCopy() instead, which does work.

However - I would LIKE to keep my persistence interactions generic. I would like a way to authorize hibernate to do the right thing automatically:
if object is loaded, then perform saveOrUpdateCopy()
if object is NOT loaded, then perform saveOrUpdate()

I can "manually" evict ahead of time, but that requires either knowledge of the "id", or I can manually choose which action to perform, but then my system is easy to break when adding logic before the save portion.

This is similar to the problem described in this thread:
http://forum.hibernate.org/viewtopic.ph ... texception

Any ideas on how to work around this?

Thanks
Tyson


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 12:11 pm 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
>> checking permissions or auditing the save.

this kind of stuff is best handled in an AOP fashion, IMHO. I coded up a good version of this using Spring because I had these sorts of problems. It works great.


Top
 Profile  
 
 Post subject: Re: saveOrUpdateCopy conundrum
PostPosted: Sat Apr 23, 2005 1:01 am 
Regular
Regular

Joined: Thu Jul 01, 2004 12:13 am
Posts: 68
Location: San Diego, CA
tysonnorris wrote:
So, I could use Session.saveOrUpdateCopy() instead, which does work.

However - I would LIKE to keep my persistence interactions generic. I would like a way to authorize hibernate to do the right thing automatically:
if object is loaded, then perform saveOrUpdateCopy()
if object is NOT loaded, then perform saveOrUpdate()


I found this thread useful, but I don't know why you'd need to check if it is loaded or not. From the Hibernate javadocs, it looks like it does do the right thing automatically. However, if you really wanted to there is a .contains() method on the session that you could use to see if the object is already there.

Best,
Lou


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.