-->
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.  [ 6 posts ] 
Author Message
 Post subject: Custom insert or update?
PostPosted: Sun May 28, 2006 7:54 am 
Newbie

Joined: Sun May 28, 2006 7:41 am
Posts: 2
I have a general question concerning inserting records using hibernate.

I have a table with several columns that would make the "business" primary key and an id column as technical primary key.

As I don't want records doubled while inserting, using jdbc I would first check if the record I am going to insert, is in the table already using a select statement targeting the "business" primary key. If I find such a record, I would update it, otherwise insert it into the table.

One could say, make your business primary a technical composite id, but i don't like semantik keys.

How would I do that in hibernate? Is their a way to use a custom saveOrUpdate-Method telling hibernate to check coloumn 1 and column 2 and if they are equal just update?

Thanks for your help!

Rudolf


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 28, 2006 9:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
session.saveOrUpdate(yourObject);

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 28, 2006 1:55 pm 
Newbie

Joined: Sun May 28, 2006 7:41 am
Posts: 2
Thanks for replying. SaveOrUpdate won't work here, because I am not talking aboutthe primary key. Example: A table "address" with id, name, street, zip. Id is the primary key.

When I create a new address object, I first want to check if one with the same name and zip is already in the db. as far as I know Hibernate checks whether the primary key is already been used.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 28, 2006 2:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so you will have to do that manually - check if it is already there and use that object instead.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 28, 2006 6:09 pm 
Beginner
Beginner

Joined: Fri Mar 17, 2006 7:30 pm
Posts: 32
In your DAO layer, construct a query via Hibernate that searches on the criteria you want. If you get an object back, you know you are just updating. Either copy the properties from your "does it need to be saved bean" to the one just retrieved, or used the Id from the retrieved one and set it on the to be saved. Then call updateOrSave(). The latter allows you to use the same method call for both a save new or update old.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 29, 2006 2:45 am 
Newbie

Joined: Mon May 29, 2006 12:57 am
Posts: 11
Location: india
Hi rosemeyer2,

I guess u have to manually fire a query to check if the row exists in the table.

In case it isn't , u can always fire a query.save()

Otherwise, u can fetch it and then change the attributes required and then doa session.merge() instead of session.update()

U will avoid lots of exception related to dirtyChecks or detached objects in the session.

Any comments????


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