NHibernate: DetachedCriteria, nesting more than once

I cannot get DetachedCriteria to work properly after nesting several times, the reason is that DetatchedCriteria can only access items with an alias one level higher.

The following steps do not work, for example:

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>();

Does anyone know a workaround that is not related to using HQL?

+3
source share
1 answer

I have never had this problem, I usually use PropertyIn to join subqueries, but you cannot always do the same.

In this particular case, you can fix this 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");

, . , .

0

Source: https://habr.com/ru/post/1791938/


All Articles