O'Reilly logo

NHibernate 3 Beginner's Guide by Gabriel N. Schenker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Bulk data changes

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(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required