Note that there are some explanatory texts on larger screens.

plurals
  1. PONhibernate updating version column on foreign key entity
    primarykey
    data
    text
    <p>I am using c# with Fluent NHibernate and auto mapping.</p> <p>Here is some code (truncated for clarity), then I'll explain the problem.</p> <pre><code>public class Company { public virtual string Description { get; set; } } public class Stock { public virtual Product Product { get; set; } public virtual Company Company { get; set; } } </code></pre> <p><strong>Mapping</strong></p> <pre><code>mappings.Conventions.Add&lt;CascadeConvention&gt;() .Conventions.Add&lt;CustomForeignKeyConvention&gt;() .Conventions.Add&lt;HasManyConvention&gt;() .Conventions.Add&lt;VersionConvention&gt;() </code></pre> <ul> <li><code>CascadeConvention</code> just sets everything to All.</li> <li><code>CustomForeignKeyConvention</code> removes the _id that NHibernate usually appends to foreign key id columns.</li> <li><code>HasManyConvention</code> sets all <code>HasMany</code>'s to inverse.</li> <li><p><code>VersionConvention</code> convertion looks like this:</p> <p>instance.Column("Version");</p> <p>instance.Default(1);</p></li> </ul> <p>The problem is that when I insert a new stock record, Nhibernate also updates the version number on the related <code>Company</code>. If I had an <code>IList&lt;Stock&gt;</code> property on the <code>Company</code> then that would make sense but I don't.</p> <p>I've done a lot of reading around:</p> <ul> <li><a href="https://stackoverflow.com/questions/11150260/nhibernate-emitting-extraneous-update-statements-regardless-of-proper-inverse-f">NHibernate emitting extraneous update statements regardless of proper Inverse (fluent nhibernate) settings on relations</a></li> <li><a href="http://frankmao.com/2011/05/02/cascade-save-update-in-nhibernate-and-version-column/" rel="nofollow noreferrer">Cascade Save-Update in NHibernate and version column</a></li> <li><a href="http://docs.huihoo.com/hibernate/nhibernate-reference-1.2.0/example-parentchild.html" rel="nofollow noreferrer">NHibernate Reference - Chapter 17. Example: Parent/Child</a></li> <li><a href="http://ayende.com/blog/3936/nhibernate-mapping-property" rel="nofollow noreferrer">Ayende @ Rahien - NHibernate Mapping</a></li> </ul> <p>From these, I've tried a whole bunch of things including adding <code>.Not.OptimisticLock()</code> all over the place. I even added an <code>IList&lt;Stock&gt;</code> property on <code>Company</code> so that I could specifically set it as <code>Inverse</code>, <code>Not.OptimisticLock</code>, etc. Nothing I do seems to make any difference.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload