R – how would I convert this join using Criteria query

criterianhibernate

my create query (HQL) looks like:

Session.CreateQuery(
   "select a from Article as a join a.Categories c where c.ID = :ID")
   .SetInt32("ID", categoryId)

Best Answer

Straight forward it looks like this:

ICriteria query = Session
  .CreateCriteria<Article>("a")
  .CreateCriteria("a.Categories", "c")
  .Add(Expression.Eq("c.ID", categoryId)

There is also the IdEq expression,but you can't say which id (of which entity / alias name), so I don't trust it.

ICriteria query = Session
  .CreateCriteria<Article>("a")
  .CreateCriteria("a.Categories", "c")
  .Add(Expression.IdEq(categoryId) // a.ID or c.ID?

There is a problem with joins to collections. You'll get as many results as the join of article and categories will produce, this could multiply the articles.

Use either the result transformer or subqueries. See this post.

Related Topic