NHibernate 2.1: LEFT JOIN on SubQuery with Alias (ICriteria)

你说的曾经没有我的故事 提交于 2019-11-30 21:18:42

Unfortunately, Criteria is a bit restricted.

Try this:

session.CreateCriteria(typeof(SomeTable), "st")
  .SetProjection( Projections.ProjectionList()
    .Add(Projections.GroupProperty("st.id"))
    .Add(Projections.GroupProperty("st.NonAggregateColumn"))
    .Add(Projections.RowCount(), "rowcount")
    .Add(Projections.Avg("at.YetAnotherNonAggregate"), "avg"));
  .CreateCriteria( "st.OtherTables", "at", JoinType.InnerJoin)
  .List<object[]>();

You probably need to play around a bit, it's more of a guess. It also might be impossible this way.

It should produce something like this:

select
  st.id,
  st.NonAggregateColumn,
  count() as "rowcount",
  avg(at.YetAnotherNonAggregate) as "avg"
from
  SomeTable st inner join AnotherTable at on ...
group by
  st.id,
  st.NonAggregateColumn

Generally:

  • You can make subqueries using DetachedCriteria. See the docs for more details.
  • You can't make a cartesian product with Criteria and filter in the where clause. (This only works with HQL).
  • Subqueries can not be added to the from clause (because that would result in a cartesian product). You can only put them to the where clause (in, exists etc.)
  • You could probably start with AnotherTable and navigate to SomeTable. This might be a alternative solution.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!