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…
Add a new folder named
HqlBulkChanges
to the project.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 b.UnitPrice = :minPrice where b.UnitPrice < :minPrice"; var updated=session.CreateQuery(hql) .SetDecimal("minPrice", 55M) ...