Querying with NHibernate

I am new to NHibernate, so this is the main question.

When retrieving data from a database through an NHibernate session, I only did this using Id so far, for example. eg:

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

But how do I get an object based on a property that is not an identifier? For instance. pull the client out by searching in the Name property. This may return 0-n responses, so I assume I will return the list to me?

+3
source share
4 answers

Using HQL, it will look something like this:

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

Another option is Linq2NHibernate , which has been getting more and more mature lately.

: >

var employees = Session.Linq<Employee>()
    .Single(employee=>employee.Name==name);

Linq INHibernateQueryable, IQueryable.

+4

Basically, you have two options: HQL and criteria API . The Hibernate documentation is fantastic, so I highly recommend that you read it and not leave an unnecessary detailed answer.

+2
source

There are many ways to query NHibernate:

  • Hql
  • Criteria
  • Request for example
  • Request Criteria
  • LINQ (2 different linq providers available)
  • H-sql
  • SQL
  • You can implement your own request method.
+2
source

Source: https://habr.com/ru/post/1726932/


All Articles