NHibernate: DetachedCriteria nesting more than once
I can't get DetachedCriteria to work properly after nesting more than once, the reason being that the DetatchedCriteria can only access aliased entities one layer higher.
The following doesn't work, for example:
var secondNestedCriteria = DetachedCriteria.For<Baz>("baz")
.SetProjection(Projections.Id())
.Add(Restrictions.EqProperty("baz.FooName", "foo.Name") // Doesn't work
.Add(Restri开发者_如何学JAVActions.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>();
Does anyone know a workaround that doesn't involve using HQL?
I never had this problem, I usually use PropertyIn to join subqueries, but you can't always do the same.
In this particular case, you could fix it by using the property of the second query:
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");
If this doesn't help in your real case, you may ask for a solution of the real case. I don't think that there is a general solution.
精彩评论