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 5/10/2009 1:31 PM
Gravatar
loved it

# re: HOWTO: Create a UNIQUE constraint using NHibernate

Left by מדבקות קיר at 6/9/2009 6:18 AM
Gravatar
LOVE IT

Your comment:



 (will not be displayed)


 
 
 
Please add 3 and 3 and type the answer here:
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678