在多次嵌套之后,我无法让DetachedCriteria正常工作,原因是DetatchedCriteria只能访问更高一层的别名实体。
例如,以下代码不起作用:
var secondNestedCriteria = DetachedCriteria.For<Baz>("baz")
.SetProjection(Projections.Id())
.Add(Restrictions.EqProperty("baz.FooName", "foo.Name") // Doesn't work
.Add(Restrictions.EqProperty("baz.BarName", "bar.Name");
var firstNestedCriteria = DetachedCriteria.For<Bar>("bar")
.SetProjection(Projections.Id())
.Add(Restrictions.EqProperty("bar.FooName", "foo.Name")
.Add(Subqueries.Exists(secondNestedCriteria);
var criteria = Session.CreateCriteria<Foo>("foo")
.Add(Subqueries.Exists(firstNestedCriteria)
.List<Foo>();有没有人知道不需要使用HQL的变通方法?
发布于 2011-02-14 18:19:28
我从来没有遇到过这个问题,我通常使用PropertyIn来连接子查询,但您不能总是这样做。
在本例中,您可以通过使用第二个查询的属性来修复它:
var secondNestedCriteria = DetachedCriteria.For<Baz>("baz")
.SetProjection(Projections.Id())
.Add(Restrictions.EqProperty("baz.FooName", "bar.FooName") // is equal to foo.Name
.Add(Restrictions.EqProperty("baz.BarName", "bar.Name");如果这对你的真实案例没有帮助,你可以要求真实案例的解决方案。我不认为有一个通用的解决方案。
https://stackoverflow.com/questions/4989875
复制相似问题