开发者

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

开发者 https://www.devze.com 2023-03-01 22:01 出处:网络
Family and Parents is a m:m relationship. Right now I have this query: public Family GetFamilyByParentId(long parentId)

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消