1

I am trying to update several records with a database through NHibernate.

The direct SQL query looks like:

update records set sortOrder = sortOrder +1 where sortOrder >= 3 and sortOrder <= 100

Is this possible in NHibernate? I don't want to take the approach of pulling each record and updating them one at a time since this method can be used on databases with several thousand records.

1 Answers1

2

It sounds like you just want to send a command to the DB to do the update. If that's the case, you can use the CreateSQLQuery method on the session object to do so. More info here.

If you are trying to persist multiple objects at once, you'll need to do a batch update. More info on this from here.

Community
  • 1
  • 1
LordHits
  • 5,054
  • 3
  • 38
  • 51
  • Is CreateSQLQuery platform independent? I thought I read where Criteria > HQL > SQL in regards to platform independence, but I might be wrong. – Mike Cole Feb 18 '10 at 05:14
  • CreateSQLQuery is not platform independent. You could make calls that in theory different DBs may not support. – LordHits Feb 18 '10 at 17:14
  • That is exactly the answer I was looking for. It worked perfectly for what I was needing to do. Thanks a lot for your help. – Michael D. Kirkpatrick Feb 19 '10 at 13:39