-->
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.  [ 11 posts ] 
Author Message
 Post subject: How to define composite unique key annotation
PostPosted: Thu Jul 29, 2010 4:45 am 
Newbie

Joined: Wed May 19, 2010 6:03 am
Posts: 19
Hi All,

I have a table (USERROLE) with one primary key(UserRoleID) and two foreign keys (UserId and RoleID). I want to declare the two foreign keys together as composite unique key means, there will not be more than one row with same UserID, RoleID together.

Any one can give me some idea how this can be done by annotation ?

Thanks in Advance
Vasna


Top
 Profile  
 
 Post subject: Re: How to define composite unique key annotation
PostPosted: Mon Aug 09, 2010 12:04 am 
Newbie

Joined: Wed May 19, 2010 6:03 am
Posts: 19
Any idea please....?


Top
 Profile  
 
 Post subject: Re: How to define composite unique key annotation
PostPosted: Mon Aug 09, 2010 12:26 am 
Newbie

Joined: Tue Oct 07, 2008 12:17 pm
Posts: 13
Vasna,
I hope below post can help you in resolving the issue.

http://www.mkyong.com/hibernate/hiberna ... p-example/

--Velu


Top
 Profile  
 
 Post subject: Re: How to define composite unique key annotation
PostPosted: Wed Aug 25, 2010 1:48 am 
Newbie

Joined: Wed May 19, 2010 6:03 am
Posts: 19
Sorry Velu that is not by requirement, here the third table is a link table. My case is entirely different.

I have USER, ROLE, and QUOTA tables, In side the QUOTA I have UserID and RoleID as foreign key and QuotaID as primary key. I want to allow only one record for each UserID and RoleID together. For this I want to define UserID and RoleID together as unique key. So it will not allow to insert quota to same UserId and RoleID.

Any valuable suggestions please...


Top
 Profile  
 
 Post subject: Re: How to define composite unique key annotation
PostPosted: Fri Aug 27, 2010 12:15 am 
Newbie

Joined: Wed May 19, 2010 6:03 am
Posts: 19
Any eye opening suggestions please....


Top
 Profile  
 
 Post subject: Re: How to define composite unique key annotation
PostPosted: Fri Aug 27, 2010 1:47 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Try with the @UniqueConstraint annotation as described in the documentation:

http://docs.jboss.org/hibernate/stable/ ... ing-entity


Top
 Profile  
 
 Post subject: Re: How to define composite unique key annotation
PostPosted: Fri Aug 27, 2010 2:47 am 
Newbie

Joined: Wed May 19, 2010 6:03 am
Posts: 19
Hi nordborg,

Thanks for your reply. I have tried this in my class as follows
@UniqueConstraint(columnNames = { "QuotaID" }),
@UniqueConstraint(columnNames = { "RoleID", "UserID" })

But still I am able to insert multiple records with same RoleID and UserID combination.


Top
 Profile  
 
 Post subject: Re: How to define composite unique key annotation
PostPosted: Fri Aug 27, 2010 3:20 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Are you using Hibernate to generate the database schema? Why do you have a @UniqueConstraint on the QuotaID? It should not be needed if it is the primary key.


Top
 Profile  
 
 Post subject: Re: How to define composite unique key annotation
PostPosted: Fri Aug 27, 2010 5:02 am 
Newbie

Joined: Wed May 19, 2010 6:03 am
Posts: 19
I am not using Hibernate to generate Schema, but I want to set the Composite Unique Constraint from Hibernate itself. Because there may be some DB that may not support some specific properties that is the reason I want to move all such thing to Class.

Even after removing the PK from the constrain it is working as the same.
Now I have the Constraint is as follows
@UniqueConstraint(columnNames = { "RoleID", "UserID" })


Top
 Profile  
 
 Post subject: Re: How to define composite unique key annotation
PostPosted: Fri Aug 27, 2010 5:25 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The @UniqueConstraint (and other similar annotations) are only used by the schema generation tool supplied with Hibernate. If you create the schema in some other way you have to add the proper constraints in the CREATE TABLE statement yourself.


Top
 Profile  
 
 Post subject: Re: How to define composite unique key annotation
PostPosted: Fri Aug 27, 2010 7:06 am 
Newbie

Joined: Wed May 19, 2010 6:03 am
Posts: 19
Thanks nordborg for the information
If anybody have any other thoughts please share..


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