Is there a way in NHibernate to make it generate SQL which adds a value to a column as in update column1 = column1 + 10?
This is for concurrent updates when two connections could be updating the value and select followed by update would not be work correctly.
The other way seems to have the following lines in the mapping file:
optimistic-lock="dirty"
dynamic-update="true"
Then the code would look something like this:
byte tryAgain = 3;
while( --tryAgain >= 0 )
{
try {
using (ISession s = OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
var person = s.Query<X>().Where(o => o.Y == z ).FirstOrDefault();
if( person != null ) {
person.Column1 = person.Column1 + myValue;
s.Update( person );
}
break;
}
} catch ( StaleObjectException ) {
if( tryAgain == 0 ) {
throw;
}
}
}
The amount of code to handle this situation is not too bad, but
update column1 = column1 + x is such a nice and basic SQL feature that it would be cool if there was a way to e.g. express the fact that the field of the mapped class in the mapping represents a delta rather than an absolute value.
Thanks is advance,
Tymek
using HQL