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 a column. The problem is as follows:
I have two tables, 'Person' and 'Country'. Each Person has one and only one Country associated with it. A Country can have many Persons (really! :)) and a Countries' Name is unique. The following is the mapping on the Person side:
<many-to-one Name="Country">
<column Name="CountryId"/>
</many-to-one>
On the Country side:
<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 a Person instance NHibernate just tries to do INSERTS, whereas I would expect it to check if a Country Name exists and use its ID in the CountryID column in the Person table. Instead, an exception is thrown that results from violation of the unique constraint in the database.
It seems to me Nibernate should have enough mapping metadata to do the right thing (or does the unique attribute on the property not ensure this?). Does anyone know how to do this or have a workaround?
Thanks,
Martijn