-->
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: Unique constraint on multiple properties
PostPosted: Thu Jun 08, 2006 3:23 am 
Senior
Senior

Joined: Sat Sep 03, 2005 12:54 am
Posts: 139
Hi,

I am using the schema export feature quite extensively as I develop my domain and was wondering if it is possible in the mapping files to create a unique constraint/index across multiple properties. For example, suppose I have the following 2 properties in an Order object:

Code:
<property name="ReferenceNumber" column="ReferenceNumber" type="String" length="20" not-null="true" />
<many-to-one name="Customer" column="CustomerID" class="SomeDomain.Customer, SomeDomain" not-null="true" />


I want to ensure that the order reference number is unique within each customer and would like that constraint created in the DB as well as doing the validation in my domain. Is this possible in the mapping file or do I just have to add those types of constraints in the DB manually?

Cheers,

Jason


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 8:32 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Using unique="true" should do it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 5:51 pm 
Senior
Senior

Joined: Sat Sep 03, 2005 12:54 am
Posts: 139
Hi Sergey,

I have already tried adding that attribute to the mappings but nothing extra gets generated in the SQL...is this a bug? I couldn't find anything in the doco's about the use of the attribute on the property element either although it does mention it for the assocation elements.

Will the unique attribute look at multiple properties to generate a single unique constraint or will it generate one unique constraint per property?

Cheers,

Jason


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 10, 2006 9:15 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Hmm, it should have worked. Which dialect are you using?

You can also try specifying <column unique-key="SomeName" />, then one constraint will be created for all the columns that share the same unique-key value.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 10, 2006 7:14 pm 
Senior
Senior

Joined: Sat Sep 03, 2005 12:54 am
Posts: 139
Sorry Sergey, it does currently add the unique attribute to each column in the SQL when you specify unique="true" on the property...not sure how I missed this before!

The unique-key attribute on the column element is exactly what I am looking for though...thanks.

Jason


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.