.netnhibernate

Doing queries with NHibernate


I'm new to NHibernate, so this is a basic question..

When fetching data from the database through an NHibernate session I've only done it using the Id this far, e.g. like this:

var customer = Session.Get<Customer>(customerId); 

But how do I fetch an object based on a property that isn't the Id? E.g. fetch a customer by search on the Name property. This could return 0-n answers, so I'm assuming I would get a list back?


Solution

  • Using HQL it would look something like:

    session.CreateQuery("from Customer where Name=:name")
           .SetString("name", name)
           .List<Customer>();