NHibernate mistakenly compares with null when a component is removed

I am currently having a problem updating the collection of components for an object. It was initially displayed as a bag, but this caused all entries to be deleted and reinserted each time. Changing it in the kit fixed this problem, but introduced a new one.

The type of component is called Tracking , it has a composite key UserID and ItemID and two properties, which are dates with a zero value. When one of them is created DateRead set to the current time, it will then be replaced later by a record with a new date.

In base NHibernate SQL, a where clause is generated that checks that all properties match.

The problem is that the other DateAcknowledged date DateAcknowledged often null, and the generated SQL seems to have a syntax error to do a null check: = NULL and not: IS NULL , as shown:

 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 */ 

The fact is that two dates are not needed at all to determine what to delete. Just having a validator id and user id.

Here is the mapping code where I define the set:

 Set(x => x.Trackings, mapper => { mapper.Key(k => k.Column("ItemId")); mapper.Table("Tracking"); mapper.Access(Accessor.NoSetter); }, collectionMapping => collectionMapping.Component(TrackingMap.Mapping())); 

And here is the mapping for the component:

 public class TrackingMap { public static Action<IComponentElementMapper<Tracking>> Mapping() { return c => { c.ManyToOne(x => x.User, a => { a.Column("UserId"); a.ForeignKey("UserId"); }); c.Property(x => x.DateRead); c.Property(x => x.DateAcknowledged, a => a.NotNullable(false)); }; } } 

Is there a way to tell NHibernate to use keys only in the where clause or to correctly compare zeros?

+4
source share
2 answers

This is described in section 7.2. Collections of dependent objects that note that this is not supported:

Note that compound mapping does not support null-value properties if you use <set>. NHibernate must use each column value to identify the record when deleting objects (there is no separate primary key column in the component table), which is impossible with null values. You must either use only non-null properties in the compound element, or select "lt; list>, <map>, <bag> or <idbag>.

+2
source

Refer to this answer for a good explanation of how to remove NHibernate objects using criteria. This should allow you to use only ItemId and UserId when defining items to remove and safely ignore date mappings.

+1
source

Source: https://habr.com/ru/post/1393183/


All Articles