O'Reilly logo

NHibernate 3.0 Cookbook by Jason Dentler

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

Using Detached Queries

In some cases, it may be preferable to build an HQL or criteria query object in parts of your application without access to the NHibernate session, and then execute them elsewhere with a session. In this recipe, I'll show you how to use detached queries and criteria.

Getting ready

Set up a new NHibernate console application using our Eg.Core model from Chapter 1. Configure log4net to send the NHibernate.SQL debug output to the .NET trace, just as we did in Chapter 2.

How to do it...

Add the following code to your Main method:

var isbn = "3043"; var query = DetachedCriteria.For<Book>() .Add(Restrictions.Eq("ISBN", isbn)); using (var session = sessionFactory.OpenSession()) { using (var tx = session.BeginTransaction()) { var book ...

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