AFAIK, you cannot join subqueries at all in NHibernate, but you can re-arrange the query to use either EXISTS or IN to replicate the same functions.
I understand that this question requires that this be done using the criteria API, but I thought I would post a version of HQL that could give someone else some ideas.
var results = session.CreateQuery("from Product p where p.Id in ( select max(p2.id) from Product p2 group by p2.col1 )")
I also found this JIRA problem related to the Criteria API, and not including the column group in select. Currently, it seems that you cannot achieve criteria using the API at all.
Group by property without adding select clause
UPDATE Using the example from the Monkey Coders post, you can do this:
var subquery = DetachedCriteria.For<Product>("p") .SetProjection(Projections.ProjectionList() .Add(Projections.GroupProperty("p.Col1")) .Add(Restrictions.EqProperty("p2.Id", Projections.Max("p.Id")); var query = DetachedCriteria.For<Product>("p2") .Add(Subqueries.Exists(subquery));
What will create the following SQL
select * from Product p2 where exists ( select p.col1 from Product p group by p.col1 having p2.Id=max(p.Id) )
Mark perry
source share