Composite Unique Key being ignored

973 views
Skip to first unread message

markunsworth

unread,
Apr 3, 2009, 10:55:03 AM4/3/09
to Fluent NHibernate
I have set up a composite unique key on two columns of a mapping (see
below) but for some reason nHibernate still tries to insert records
which are duplicate rows.

I've looked around but there is very little in the way of help online.
Has anyone else got this working/not working?



public ProductUsageMap() {
WithTable("ProductUsage");
Id(x => x.Id).GeneratedBy.Identity().ColumnName("Id");
Map(x => x.ProductId).UniqueKey("Unique_ProductId_UsageTypeID");
Map(x => x.UsageTypeId).UniqueKey("Unique_ProductId_UsageTypeID");
Map(x => x.UsageStatusId);
}

Hudson Akridge

unread,
Apr 5, 2009, 10:56:54 PM4/5/09
to fluent-n...@googlegroups.com
First off, NHibernate won't prevent you from attempting to insert duplicate records :) It simply builds in the Unique Constraints in the database. So if you don't check for duplicates in your model, before you attempt to save back using NHibernate, you will get a Unique Key Constraint violation message back from your database.

Second, the two Id's you're mapping, how are those generated? Are they assigned through some sort of bi-directional association in your model? Typically I've seen this problem happen when you have an entity (such as your ProductUsage class) that has two parents (two many-to-one mappings, or Fluent's Reference() mapping). But in those cases, the actual classes would be mapped using Fluent, not just an Id.

Could you perhaps post your ProductUsage class data definition, and any other classes you're trying to map?
Reply all
Reply to author
Forward
0 new messages