Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate wrongly comparing to null when deleting a component
    primarykey
    data
    text
    <p>I currently have an issue when updating a component collection on an entity. It was originally mapped as a bag but that was causing all entries to be deleted and reinserted each time. Changing it to a set has fixed that problem but introduced a new one.</p> <p>The component type is called <code>Tracking</code> it has a composite key of <code>UserID</code> and <code>ItemID</code> and two properties which are nullable dates. When one of these is created <code>DateRead</code> set to the current time, it is then replaced later with an entry with the new date on. </p> <p>The underlying SQL NHibernate generates has a where clause which checks that all properties match.</p> <p>The issue is, the other date <code>DateAcknowledged</code> is often null, and the generated SQL seems to have a syntax error, to do a null check its doing this: <code>= NULL</code> rather than: <code>IS NULL</code>, as shown:</p> <pre><code>DELETE FROM TrackingTable WHERE ItemId = 'a68f6dea-1c00-42e2-bc40-9fcf01121bd8' /* @p0 */ AND UserId = 'c8aa41a4-e4c2-4347-ae6e-b48738a53b47' /* @p1 */ AND DateRead = '2012-01-26T12:56:46.00' /* @p2 */ AND DateAcknowledged = NULL /* @p3 */ </code></pre> <p>The thing is, the two dates should not be needed at all to determine what to delete. Simply having the where check item ID and user ID would do.</p> <p>Here is the mapping code where I define the set:</p> <pre><code>Set(x =&gt; x.Trackings, mapper =&gt; { mapper.Key(k =&gt; k.Column("ItemId")); mapper.Table("Tracking"); mapper.Access(Accessor.NoSetter); }, collectionMapping =&gt; collectionMapping.Component(TrackingMap.Mapping())); </code></pre> <p>And here is the mapping for the component:</p> <pre><code>public class TrackingMap { public static Action&lt;IComponentElementMapper&lt;Tracking&gt;&gt; Mapping() { return c =&gt; { c.ManyToOne(x =&gt; x.User, a =&gt; { a.Column("UserId"); a.ForeignKey("UserId"); }); c.Property(x =&gt; x.DateRead); c.Property(x =&gt; x.DateAcknowledged, a =&gt; a.NotNullable(false)); }; } } </code></pre> <p>Is there a way to tell NHibernate to use the keys only on the where clause or for it to compare nulls in the correct way?</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.
 

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