I have a problem trying to model a many-to-one relationship in NHibernate, where the object on the one side has a unique constraint on the column. The problem is this:
I have two tables: "Person" and "Country". Each person has one and only one country associated with him. There can be many people in a country (really! :)), and the name of the Country is unique. The following is a display on the Face side:
<many-to-one Name="Country"> <column Name="CountryId"/> </many-to-one>
On the side of the country:
<property name="Name" unique="true"> <column name="Name" length="50"> </property>
Now in the database, I have added a unique constraint on the Name column in the Country table. If I call Save () on an instance of Person, NHibernate will simply try to do INSERTS, while I expect it to check if the country name exists and use its identifier in the CountryID column in the Person table. Instead, an exception is thrown that occurs as a result of breaking a unique constraint in the database.
It seems to me that Nibernate should have enough display metadata to do the right thing (or does the unique property attribute not guarantee this?). Does anyone know how to do this or is there a workaround?
Thanks,
Martijn
Martijn himself
source share