tags:

views:

33

answers:

1

I have a class which has a collection

public class Parent
{
    ...
    ISet<Child> Children;
    ...
}

Given a list of child names, I'd like to return parents whose Children property contains all items of this list.

I managed to write an HQL query, but it works for a single name, not a whole list :

SELECT p FROM Parent AS p JOIN p.Children AS c WHERE c.Name = 'MyName'
A: 

Thanks Mauricio, I managed to solve this problem with the following criteria query

var criteria = session.CreateCriteria<Parent>("p")
                        .Add(Restrictions.IsNotEmpty("p.Children"));

foreach (var name in namesToMatch)
{
     criteria.Add(Subqueries.Exists(DetachedCriteria.For<Parent>("p2")
                            .SetProjection(Projections.Id())
                            .CreateAlias("p2.Children", "c")
                            .Add(Restrictions.Eq("c.Name", name))
                            .Add(Restrictions.EqProperty("p2.Id", "p.Id"))));
}
rohk