For the same reason, I used explicit AB-ID properties for all my many-to-one relationships. I do not see this as a quick and dirty solution, since it provides a solution to this problem, and flexibility is the area where updates are kept, that is, I do not need to extract object B from the database just to assign it A to create a connection, when I have a B_ID in the query string or somewhere else.
My mapping files are as follows:
<property name="CreatorID" column="CreatorID" type="Int32" not-null="true" /> <many-to-one name="Creator" column="CreatorID" class="SystemUser" insert="false" update="false" cascade="none" />
As you can see, one of the two properties should only be read to prevent NHibernate from sending this column 2 times to the database during insertions or updates. The above does read only (using the attributes insert = "false" update = "false") is ambiguous, but you can instead use the CreatorID property only if you want.
With only many-to-one, you do not have a property in the entity class A to store the value B.ID. The only way to get this is to access object B, which the proxy server will call, and it will start a database query (if it is not already loaded into the session).
I will be glad to hear any other option that provides a solution and offers the same flexibility.
tolism7
source share