John Farrell
John Farrell

Reputation: 24754

NHibernate many 2 many query, returning all of one side and its children

Family and Parents is a m:m relationship.

Right now I have this query:

    public Family GetFamilyByParentId(long parentId)
    {
        queryString = @"select f
                        from Family f
                        join fetch f.Parents p
                        join fetch p.Person per
                        where p.Id = :id";

        return Session.CreateQuery(queryString)
                    .SetInt64("id", parentId)
                    .UniqueResult<Family>();
    }

This works fine, gets me the Family by one of the other side's parentIds. The problem is this query only loads a single parent. I need it to return all of the Family's parents.

I need to use some type of subquery for this right?

Upvotes: 1

Views: 84

Answers (1)

Vadim
Vadim

Reputation: 17957

select f
from Family f
join fetch f.Parents p
join fetch p.Person per
where f.Id IN (select sub.Id 
            from Family sub join sub.Parents subP join subP.Person subPer 
            where subPer.Id = :id)

I think that will load all of the parents

Upvotes: 2

Related Questions