Using HQL for bulk data changes

In the previous chapter, we learned how to use NHibernate to insert, update, and delete individual entities using ISession methods. NHibernate also allows us to perform some bulk data changes with executable HQL. In this recipe, we'll show you how to use HQL to update all of our books with a single statement.

Getting ready

Complete the Getting Ready section at the beginning of this chapter.

How to do it…

  1. Add a new folder named HqlBulkChanges to the project.
  2. Add a new class named Recipe to the folder:
    using System; using NH4CookbookHelpers.Queries; using NHibernate; namespace QueryRecipes.HqlBulkChanges { public class Recipe : QueryRecipe { protected override void Run(ISession session) { var hql = @"update Book b set ...

Get NHibernate 4.x Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.