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 Criteria Queries

In the last chapter, we fetched our entities by their ID. In this recipe, I'll show you a few basic criteria queries to fetch entities by other properties.

How to do it...

  1. Complete the setup steps in the introduction at the beginning of this chapter.
  2. In the Queries class, add the following method:
    public IEnumerable<Movie> GetMoviesDirectedBy(string directorName)
    {
      return _session.CreateCriteria<Movie>()
        .Add(Restrictions.Eq("Director", directorName))
        .List<Movie>();
    }
  3. In the Queries class, add the following method to query for movies by actor name:
    public IEnumerable<Movie> GetMoviesWith(string actorName) { return _session.CreateCriteria<Movie>() .CreateCriteria("Actors", JoinType.InnerJoin) .Add(Restrictions.Eq("Actor", actorName)) ...

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