I've got a problem attempting to model a many-to-one relationship in NHibernate, in which the object around the 'one' side includes a unique constraint on the column. The issue is the following:

I've two tables, 'Person' and 'Country'. Each Individual has only one Country connected by using it. A Rustic might have many (really! :)) along with a Countries' Title is exclusive. This is the mapping from case to case side:

<many-to-one Name="Country">
<column Name="CountryId"/>
</many-to-one>

Around the Country:

<property name="Name" unique="true">
<column name="Name" length="50">
</property>

Now within the database I've added a distinctive constraint around the Title column in the united states table. Basically call Save() on the Person instance NHibernate just attempts to do Card inserts, whereas I'd expect it to see if a rustic Title is available and employ its ID within the CountryID column within the Person table. Rather, the best is tossed that is a result of breach from the unique constraint within the database.

It appears in my experience Nibernate must have enough mapping metadata to complete the best factor (or does the initial attribute around the property not ensure this?). Does anybody understand how to do that or possess a workaround?

Thanks,

Martijn

You have to assign a rustic instance towards the Country property of the individual instance (not only set the ID). Something similar to:

Person p = new Person();
p.Country = session.Load<Country>(countryId);
session.Save(p);

Then NHibernate will get sound advice. This won't also result in a DB hit to retrieve country, because the Load method will return a rustic proxy and also the only factor you are being able to access may be the Country instance's ID.

Perhaps you have attempted using SaveOrUpdate() ?

Yep, but that does not really make a difference. Thanks, though.

I suppose this will be a comment for your answer however i haven't enough kudos to achieve that. Also I cant edit my very own spelling mistake within the question's title. I ought to most likely register like a user :).

I essentially have a similar scenario. However I think what Martin and that i are generally after is:

Can you really get NHibernate to check out the Title property of the nation entity, see if it is available, and when it will then connect the individual using the correct country without by hand needing to check and connect? During my situation the Id property of the nation entity could be set to NHibernates "unsaved value".

Person p = new Person();
p.Country = new Country { Name = "SomeExistingCountryName" }
session.Save(p);

I'd an identical requirement and solved it using SaveOrUpdateCopy.

Allows if you have two differing people objects, with each having a reference to another Country object. As lengthy because the Country IDs are identical, you will not have an exception and just 1 Country come in the database.

The only real factor with this particular approach is that you'll want to assign an ID for your Country objects before calling SaveOrUpdateCopy.