update records with special conditions nhibernate

759 views Asked by At

I have a table(InquiryTable) and first of all I select some records from it, after I extract these records from database, I update them into database. but I need to know how I can do these two commands simultaneously(merge them in one) by nhibernate.

inquiry = Session.Query<InquiryTable>().Where((c => c.ID == ID)).ToList();    
inquiry.FirstOrDefault().Time= sendTime;
Session.Update(inquiry);

I want to merge Session.Query and Session.Update in one command. It means that I need an update with where in same query.

1

There are 1 answers

0
Radim Köhler On

What would fit to this concept is NHiberante DML

13.3. DML-style operations

As already discussed, automatic and transparent object/relational mapping is concerned with the management of object state. This implies that the object state is available in memory, hence manipulating (using the SQL Data Manipulation Language (DML) statements: INSERT, UPDATE, DELETE) data directly in the database will not affect in-memory state. However, NHibernate provides methods for bulk SQL-style DML statement execution which are performed through the Hibernate Query Language (HQL).

This feature set is not built on top of ICriteria or QueryOver, but it does use HQL.

An example from the doc, doing the UPDATE on filtered data in one shot:

string hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName";
// or string hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = s.CreateQuery( hqlUpdate )
        .SetString( "newName", newName )
        .SetString( "oldName", oldName )
        .ExecuteUpdate();

See also: