-->
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.  [ 7 posts ] 
Author Message
 Post subject: Enforce multi-column uniqueness
PostPosted: Mon Dec 26, 2005 3:43 pm 
Beginner
Beginner

Joined: Mon Dec 26, 2005 3:29 pm
Posts: 23
Hibernate version: 3.1

Hi. I am having a problem figuring out how to have Hibernate enforce uniqueness on a set of columns. Basically, we have a legacy database that contains four(!) primary keys (the reason there are four is because other applications need it). From our application's perspective, we want to define uniqueness based on two of those columns. Now if one attempts to insert a row with the same values for those two columns we would like for Hibernate to stop it. The DBMS won't do it beacuse it doesn't violate any constraints.

I have tried doing this using composite-id and it allows for duplicate entries. I also tried using natural-id and properties (in both cases, I defined a separate surrogate key - identity column) but they all allowed duplicate entries.

This is a simple scenario of one table with no associations.

Thanks you in advance.

Isaac
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 5:25 am 
Expert
Expert

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

Have you tried attribute unique-key="multicolumn_unique_key_name" in the mappings.

Check this out.

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 10:59 am 
Beginner
Beginner

Joined: Mon Dec 26, 2005 3:29 pm
Posts: 23
From my understanding of the documentation
Code:
unique_key
, as well as the
Code:
unique
attribute of
Code:
property
, is useful for DDL generation. I have legacy tables. [/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 1:37 pm 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
I do not think hibernate does any data validation checks. So you will have to do this in your application yourself.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 1:50 pm 
Beginner
Beginner

Joined: Mon Dec 26, 2005 3:29 pm
Posts: 23
You may be right. However I would argue that Hibernate should provide this because they are closer to the database. Imagine that I do a validate check and it finds that there are indeed no duplicate records (as defined by me) out there. It is possible for another user to now sneak in a record (before the actual save) thereby violating the data integrity.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 1:54 pm 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
Even if hibernate did this, your scenario is not possible to avoid without doing something on the database.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 2:01 pm 
Beginner
Beginner

Joined: Mon Dec 26, 2005 3:29 pm
Posts: 23
You are correct. I guess I'll need to lock the table. I was just hoping that Hibernate would shield me from this.


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