如何将这个HQL转换为DetachedCriteria?

Rom*_*man 6 c# nhibernate orm hibernate hql

执行此HQL语句会产生以下结果:

select t, count(s) from Submission s right join s.Topics as t GROUP BY t.Id

result[0]
    [0] topic_id, topic_name, ... 
    [1] 10

result[1]
    [0] topic_id, topic_name, ... 
    [1] 12
     .
result[n]
    [0] topic_id, topic_name, ... 
    [1] 19
Run Code Online (Sandbox Code Playgroud)

这个DetachedCriteria API产生几乎相似的结果,但没有加载主题

ProjectionList PrjList = Projections.ProjectionList();
PrjList.Add(Projections.GroupProperty("Topics"), "t");
PrjList.Add(Projections.Count("Id"));

DetachedCriteria Filter = DetachedCriteria.For<Submission>();
Filter.CreateCriteria("Topics", "t", JoinType.RightOuterJoin);
Filter.SetProjection(PrjList);

result[0]
    [0] null
    [1] 10

result[1]
    [0] null
    [1] 12
     .
result[n]
    [0] null
    [1] 19
Run Code Online (Sandbox Code Playgroud)

出于某种原因,nhibernate拒绝为结果集创建主题对象,但它确实用于HQL查询.这是为什么?

小智 3

查看 Ayende 博客上的 Detached Criteria:http://ayende.com/blog/tags/nhibernate ?page=3 它为您提供了关于左外连接和 HQL 分组的非常详细的分步指南以及背后的映射。