-->
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.  [ 5 posts ] 
Author Message
 Post subject: checking before adding a record
PostPosted: Tue May 02, 2006 7:44 am 
Newbie

Joined: Mon Jan 09, 2006 8:17 am
Posts: 19
hi,

Is there anyway of checking the database before adding a record.
I have a table where the primary key is self generated. Each time i want to add a record, i need to make sure that only one 'buyer id' (a field in the table) is allowed to be inserted. Duplicate 'buyer id' values are not allowed.
Can hibernate do that or do i just need to do a read before the insertion ?

Thanks.
tony

Hibernate version: 3.1


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 11:26 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Put 'unique="true"' on the buyer_id mapping. You should do a find-by-buyer-id first though, as that's the row you'll be updating if the id isn't unique.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 1:40 am 
Beginner
Beginner

Joined: Fri Apr 28, 2006 7:48 am
Posts: 20
Hi.
Just by adding unique="true", doesnt make the buyer_id column unique.
A unique constraint has to be added to the column. When a duplicate value is added the database will throw an exception


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 5:23 am 
Newbie

Joined: Mon Jan 09, 2006 8:17 am
Posts: 19
hi,

thanks for the info. i am confused. mind explaining a little more ?

regards
tony


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 6:34 am 
Beginner
Beginner

Joined: Fri Apr 28, 2006 7:48 am
Posts: 20
What I was trying to say is adding unique="true" in your mapping files for one-one relationships would not really help you. You can add duplicate values inspite of unique="true". What you really have to take care is add a unique constraint on the column and make unique="true" in your mapping files.

You can catch the unique constraint exception which implies that the value is already present.

With this you can actually avoid writing a select before an insert statement.

Hope this helps

Regards,
Fal


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