假设我有一个这样的实体
public class Something
{
public int Id{get;set;}
public string Name{get;set;}
public IList<SomeOther> Assosiation{get;set}
}
我如何使用 Criteria API 查询 nhibernate 以获取具有超过 10 个关联的所有 Something 实体?
干杯 科林
最佳答案
类似这样的事情:
var dc = DetachedCriteria.For<SomeOther>()
.SetProjection(Projections.GroupProperty("Something.Id"))
.Add(Restrictions.Gt(Projections.RowCount(), 10));
var criteria = session.CreateCriteria(typeof (Something))
.Add(Subqueries.PropertyIn("Id", dc));
这会产生这样的结果:
SELECT this_.Id as Id7_0_,
this_.Title as Title7_0_
FROM Something this_
WHERE this_.Id in (SELECT this_0_.SomethingId as y0_
FROM SomeOther this_0_
GROUP BY this_0_.SomethingId
HAVING count(* ) > 10 /* @p0 */)
关于nhibernate - 使用 nhibernate 获取关系数大于 x 的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1071556/