-->
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: How to force unique value?
PostPosted: Wed May 25, 2005 3:26 pm 
Newbie

Joined: Wed May 25, 2005 3:10 pm
Posts: 1
I'm new to Hibernate and maybe I'm off on a wrong track. Please set me straight.

The behavior I want in my application is I create a number of things and stick 'em in a database using Hibernate. If they're already there, skip the insert.

A field in the database table is "uniqueIdentifier" and I've set it to be "unique". In my Hibernate file, I have:
<property name="uniqueIdentifier" unique="true" />

My assumption was that Hibernate would deal with the issue if I attempt to insert a new record that already has been entered. Instead, I'm getting either an SQLException or a Hibernate exception when it tries to insert a record that's already in the database.

Am I misunderstanding the purpose of "unique"? Is this a way to keep duplicate non-primary-key values from getting into the database? If not, what's the 'hibernate way' of doing a series of inserts such that no two records in the database will end up with the same value in a particular field without throwing an exception (which seems to make the Session upset)?

Thanks in advance. hbm and sql statements are below.

Dave

=====================
<hibernate-mapping>
<class name="newsprint.MyNewsArticle" table="ARTICLE">
<id name="articleId" type="int" column="articleId" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="rawHtml"/>
<property name="cleanText"/>
<property name="publicationId"/>
<property name="pubDate"/>
<property name="headline"/>
<property name="uniqueIdentifier" unique="true" />
</class>
</hibernate-mapping>


=====================
CREATE table article (
articleId int(11) PRIMARY KEY NOT NULL auto_increment,
publicationId int(11),
headline varchar(255),
rawHtml text,
cleanText text,
uniqueIdentifier varchar(255) unique,
pubDate date
);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 10:49 am 
Newbie

Joined: Thu May 26, 2005 6:00 am
Posts: 3
I have the same problem, but no answer , does anyone know ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 12:49 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Why not try saveOrUpdate?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 12:54 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Oh, I just relooked at this. I don't think the desired behaviour would be to skip records that duplicated a property just like the DB doesn't skip inserted records with a unique constraint.

I haven't really played with the unique attribute from Hibernates side but I assume it's an optimization within the session to trap the error before wasting the DB's time.

What's your business case for skipping these records? I would think it might get 'weird' if someone tried to insert a record and it just simply went into never never land, no?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 02, 2005 12:29 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi

I too wanted to use this functionality of unique multiple column.


As per Vampboy for calling saveorUpdate()

Consider i have record

item_id item_name item_price
1 computer 100
2 system 100


consider i update item_id 2 with item_name as computer to system.
then the values of the other two column are duplicated.
And i also want to tell you that i am not showing the customer about the item_id, that is only for my reference.
So according to this the value is duplicated.

I can prevent this only by unique constraint.
I have do this contraint in the Database but how can do it using Hibernate.

Any ideas to make multiple columns unique?

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


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.