I would like to append my answer, coming from my experience: Prefer Criteria
or QueryOver
API over HQL.
This is article is the starting point for argumentation: http://ayende.com/blog/4042/nhibernate-queries-should-i-use-hql-or-criteria. A small extract, cite:
... HQL is how NHibernate exposes most of its power... Its similarity to SQL means that it is often very intuitive when it comes the time to write and read it
Its biggest weakness, however, is when you want to perform queries using some dynamic criteria. For example, if you want to have a search page with multiple optional search fields. For that scenario, we have the Criteria API, which allow us to dynamically, painlessly and easily compose queries. ...
I would like to sign off the second paragraph! Sooner or later, you will move your Application (server part / data layer) to some point, where you will do some general stuff. Common paging, ordering, filtering...
Criteria resp QueryOver over HQL
To create valid HQL will require more and more effort, because you will be doing almost all the job yourself.
With the Criteria
/QueryOver
API, you are just calling standard methods Join
, Where
, OrderBy
....
Do not worry how to put all the pieces together (e.g. WHERE x AND y ... the injection of " AND " operator).
So, do know HQL, but learn Criteria resp QueryOver API... and use THEM
EDIT: Projections in Criteria world are fun.
What's more important, they can be encapsulated into objects, custom projections or custom bundles of projections. Therefore we can gain reusable code, which can hide some complexity of the implementation. But because they are objects, we can Test them and later count on them
And we also can implement custom Transformer, which could bring even more benefits. See example here
Some examples of Projections:
https://stackoverflow.com/a/18885527/1679310