Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate Where Clause

I have to write a query in Fluent NHibernate for

Select * from Users where UserName = 'Abcd' AND Password = '123456'

How to create above query with session.CreateCriteria

like image 453
Saad Avatar asked Jul 12 '11 04:07

Saad


1 Answers

Fluent NHibernate is a alternative Mapping framework to the default HBM files (Hibernate Mapping)

NHibernate itself offers different Querying API's like

  • HQL - Hibernate Querying Language
  • Criteria
  • Query - (LINQ Equivalent)
  • QueryOver - (strongly typed Criteria)
  • SQL

For Criteria your query would be something along the lines of:

var users = session.CreateCriteria<Users>()
                   .Add(Restrictions.Eq("UserName", "Abcd"))
                   .Add(Restrictions.Eq("Password", "123456"))
                   .List<Users>();

Query:

var users = from u in session.Query<Users>()
            where u.UserName == "Abcd"
            && u.Password == "123456"
            select u;

or

var users = session.Query<Users>()
                   .Where(x => x.UserName == "Abcd" && u.Password == "123456");

QueryOver:

var users = session.QueryOver<Users>()
                   .Where(x => x.UserName == "Abcd")
                   .And(u.Password == "123456")
                   .List();
like image 122
Phill Avatar answered Oct 31 '22 11:10

Phill