Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Criteria Restriction vs Expression

Tags:

If I search for NHibernate Criteria API query examples in internet there are examples that use Restrictions and others use Expression. What are the differences between those two?

For example:

posts = session.CreateCriteria<Post>()     .Add(Expression.Eq("Id", 1))     .List<Post>();  posts = session.CreateCriteria<Post>()     .Add(Restrictions.Eq("Id", 1))     .List<Post>(); 
like image 735
Darius Kucinskas Avatar asked Mar 30 '11 07:03

Darius Kucinskas


People also ask

What is criteria NHibernate?

The NHibernate Query by Criteria API lets you build a query by manipulating criteria objects at runtime. This approach lets you specify constraints dynamically without direct string manipulations, but it doesn't lose much of the flexibility or power of HQL.

What is the use of createAlias in hibernate?

createAlias. Join an association, assigning an alias to the joined association. Functionally equivalent to createAlias(String, String, int) using CriteriaSpecification. INNER_JOIN for the joinType.


2 Answers

I think Restrictions were released in NH2 and is now the favoured way.

According to Resharper whenever I use Expression I get a hint to say Access to a static member of a type via a derived type

Also according to this post by Ayende:-

Prefer to use the Restrictions instead of the Expression class for defining Criteria queries.

like image 62
Rippo Avatar answered Oct 11 '22 07:10

Rippo


In the source code for namespace NHibernate.Criterion.Expression is says that "This class is semi-deprecated use Restrictions"

like image 20
Benjamin Avatar answered Oct 11 '22 09:10

Benjamin