Mr. TA
Mr. TA

Reputation: 5359

NHibernate doesn't save one property only

I'm trying to update a single column in the table. No UPDATE issued whatsoever (checked using SQL Profiler). No errors.

Upvotes: 2

Views: 1906

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52745

Based on the comments, what you are trying to do is create a proxy without getting the current state from the DB (using session.Load), then issue a dynamic update.

This will not work. NHibernate does not record individual changes to properties; instead, it:

  • Compares the current state of tracked entities against a snapshot of the property values that were retrieved from the database.
  • Based on that, determines which entities are dirty (i.e. have changes)
  • It generates the SQL update statement. If using dynamic-update, it will only contain the modified properties.

If you use session.Load, as soon as you access the Deleted property, NH will load the record from the database. Whether you use session.Load or session.Get, session.Update on an already tracked entity is a no-op.

Alternatively, if you already have the state for all the order properties, you can update without loading:

var order = GetTheOrderFromMemory();
order.Deleted = true;
session.Update(order);
transaction.Commit();

This will not respect dynamic-update, as NH doesn't have a snapshot to compare with...

...which is why HQL is the only way to do a one-shot update of an individual property.

Upvotes: 4

Related Questions