tags:

views:

53

answers:

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.

+2  A: 

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.

LordHits
Is CreateSQLQuery platform independent? I thought I read where Criteria > HQL > SQL in regards to platform independence, but I might be wrong.
Mike C.
CreateSQLQuery is not platform independent. You could make calls that in theory different DBs may not support.
LordHits
Is there a way to perform the same update through HQL?
Mike C.
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