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.  [ 4 posts ] 
Author Message
 Post subject: saveOrUpdate w/ Surrogate Key. Detect Dup Based On Nat. Key?
PostPosted: Mon May 05, 2008 10:15 pm 
Newbie

Joined: Tue Nov 13, 2007 8:11 pm
Posts: 7
I have an entity where the natural id is made up of 3 columns. Since there are many relationships to and from this entity, I’ve simplified my data model and Hibernate config (allegedly) by using a surrogate key.

In the UI users can create new instances of this entity, add some child objects and, based on the natural id, they can end up creating duplicates.

I’d like to detect these duplicate transient instances on saveOrUpdate() and update the DB accordingly. Unfortunately, since I am using a surrogate key, this won’t work unless I use the 3 column natural id as the primary key.

What do you guys suggest?

I’ve considered creating a event listener, checking for the given entity name, and doing the updates by hand. Preferably I’d like to use an interceptor, as I can limit its scope by applying it in the class performing the saveOrUpdate() call, yet this might require more coding (?) since I have DAOs without transaction demarcation. Using openSession() does not attach to the current thread out-the-box.

My other option (aside from dropping the surrogate key) is to
do the updates by hand in the class that calls saveOrUpdate() prior to making actually making the call.

I’d appreciate any thoughts on this.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 3:46 am 
Regular
Regular

Joined: Mon Aug 06, 2007 10:49 am
Posts: 67
Location: Banska Bystrica, Slovakia
u can create unique index/alternative key in db


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 06, 2008 12:20 pm 
Newbie

Joined: Tue Nov 13, 2007 8:11 pm
Posts: 7
Quote:
u can create unique index/alternative key in db


I already have a unique index in the surrogate key. SaveOrUpdate() -merge really, looks at the entities id. Changing that behavior would involve changing the id or creating an event listener or interceptor.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 07, 2008 3:58 am 
Regular
Regular

Joined: Mon Aug 06, 2007 10:49 am
Posts: 67
Location: Banska Bystrica, Slovakia
what is your strategy for creating id? if u use some db generation approach it should work


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.