HOWTO: Create a UNIQUE constraint using NHibernate

I got an email question yesterday about how to create a UNIQUE contraint on a table using NHibernate.  After some research on the NHibernate Forum and digging through the NHibernate source code, I was able to find a solution.

(NOTE: my blog engine, .Text doesn't unfornately allow me to put html or xml directly in the text of a post so I'm putting element names in quotes.)

Usually, when you're mapping properties you only deal with the “property” element but “property” has an optional child element, “column” that lets you specify more detail for how NHibernate handles the column mapping.  One of the attributes is “unique-key” and it lets you define a named, unique grouping of properties/columns.  Set the unique-key attribute to the same value for all properties you want to group together and Voila! a UNIQUE constraint. 

Here's a sample class mapping with a UNIQUE constraint on FirstName and LastName. 

-Ben

 

 

 

posted @ Sunday, April 02, 2006 12:16 PM

Print

Comments on this entry:

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by DataSmithy at 7/19/2006 10:41 PM
Gravatar
I guess the advantage to this is that it validates the unique key on the .NET side, and generate a .NET error instead of a SQL error if you try and insert a duplicate value?

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by koko at 8/27/2006 9:29 PM
Gravatar
Regarding the unique-key attribute, I specified it in my class, however I can still succesfully save duplicates for that specified unique key.

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by Rich Cox at 9/27/2006 10:50 PM
Gravatar
I just researched this today, the 'unique' attribute is only used when generating DDL from mapping files. It doesn't cause any enforcement of uniqueness in the poco, or NHibernate layers...

Rich C.

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by ביטוח רכב at 10/20/2006 8:58 AM
Gravatar
I guess the advantage to this is that it validates the unique key on the .NET side, and generate a .NET error instead of a SQL error if you try and insert a duplicate value?

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by ביטוח רכב at 11/16/2008 4:14 AM
Gravatar
Great mind thinking
We r with u

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by הכשרה מקצועית at 1/26/2009 10:58 AM
Gravatar
very good article

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by בגדי תינוקות at 1/26/2009 11:07 AM
Gravatar
love your writing

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by טיסות לתאילנד at 3/2/2009 7:30 AM
Gravatar
i going to bookmark

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by טיסות לתאילנד at 3/22/2009 5:51 AM
Gravatar
loved it

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by מכונות שטיפה at 3/22/2009 11:48 AM
Gravatar
Great mind thinking
We r with u

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by אדריכלים at 2/28/2010 4:32 PM
Gravatar
the 'unique' attribute is only used when generating DDL from mapping files

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by Niklas Hedin at 4/10/2010 6:43 PM
Gravatar
Hi

Why not use the <natural-id> mapping tag? This will create a unique index of the properties listed inside this element

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by כסף at 5/1/2010 8:08 PM
Gravatar
would <natural-id> tag help?

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by משחקים ברשת at 7/24/2010 4:12 PM
Gravatar
very good article =]

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by אלרגיות at 8/9/2010 4:01 AM
Gravatar
לאנשים הסובלים מאלרגיה, כל אלו מהווים מכשול ופגיעה באיכות החיים ובתפקוד היומיומי.

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by בית דפוס at 8/12/2010 9:01 AM
Gravatar
בית דפוס בתל אביב המתמחה בתחומי הדפסת פנקסים, פליירים, הדפסת חוברות, צילום מסמכים, הדפסת כרטיסי ביקור, נייר מכתבים, מעטפות ומוצרי דפוס נוספים.

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by דרושים מהנדסים at 8/25/2010 8:47 AM
Gravatar
great article , I was stuck ,and now I'm not stuck anymore!!!

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by דרושים מהנדסים at 8/25/2010 8:57 AM
Gravatar
Forgot to tell you that if you have other comments on this topic , please post them !

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by דרושים מהנדסים at 8/25/2010 9:05 AM
Gravatar
Could anyone post another example ??

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by חברות בניה at 8/25/2010 9:22 AM
Gravatar
Great Post Thanks .

Your comment:



 (will not be displayed)


 
 
 
Please add 3 and 5 and type the answer here:
 

Live Comment Preview:

 
«September»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789