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' AND UserId = 'c8aa41a4-e4c2-4347-ae6e-b48738a53b47' AND DateRead = '2012-01-26T12:56:46.00' AND DateAcknowledged = NULL
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?