In previous chapters we learned how we can—with the aid of NHibernate—add new records to the database, change, and delete existing ones. These operations have been executed on a single record base. This is the expected behavior and fulfils our requirements in most cases. However, sometimes we would like to execute changes on a set of data as a whole in one go. NHibernate allows us to execute some bulk data changes by using HQL. For this purpose, we can use the
ExecuteUpdate method, which is defined on the
IQuery interface. Let's try to update the unit price of all products in the system with one single query, as shown in the following code snippet:
var hql = "update Product p set p.UnitPrice = 1.1 * p.UnitPrice"; session.CreateQuery(hql).ExecuteUpdate(); ...