nHibernate有很多很多都没有获取检索对象上的子对象列表

时间:2012-06-21 16:59:59

标签: nhibernate many-to-many

        // load the client and any many to one relationships
        var clientRootQuery = session.QueryOver(() => clientAlias);
        clientRootQuery.Left.JoinAlias(() => clientAlias.Person, () => personAlias)
        .Left.JoinAlias(() => personAlias.SexType, () => sexTypeAlias)
        .Left.JoinAlias(() => personAlias.EyeColorType, () => eyeColorTypeAlias)
        .Left.JoinAlias(() => clientAlias.Organization, () => organizationAlias)
        .Left.JoinAlias(() => clientAlias.ClientStatusType, () => clientStatusTypeAlias)
        .Left.JoinAlias(() => clientAlias.Notes, () => noteAlias)
        .Left.JoinAlias(() => noteAlias.Comments, () => commentAlias)
        .Where(() => clientAlias.Id == clientId)
        .Future<Client>();

        // load the note collection into the nhibernate session 
        // todo ******************** this doesn't work.  nhibernate is still firing off queries in the adapter fill method rather than using the values pulled here.
        var notes = session.QueryOver(() => clientAlias)
        .Left.JoinAlias(() => clientAlias.Notes, () => noteAlias)
        .Left.JoinAlias(() => noteAlias.Comments, () => commentAlias)
        .Where(() => clientAlias.Id == clientId)
        .Future<Client>();

return clientRootQuery.Take(1).SingleOrDefault();

这不会返回客户端对象上的许多地址,也不会返回许多注释。这应该工作。有一个客户端有许多笔记和许多地址。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

未初始化clientAddresses的原因是因为您对它们设置了过滤器,因此NHibernate假定并非所有clientAddresses都被加载以完全初始化集合。

答案 1 :(得分:0)

var clientRootQuery = session.QueryOver(() => clientAlias)
        .Left.JoinAlias(() => clientAlias.Person, () => personAlias)
        .Left.JoinAlias(() => personAlias.SexType, () => sexTypeAlias)
        .Left.JoinAlias(() => personAlias.EyeColorType, () => eyeColorTypeAlias)
        .Left.JoinAlias(() => clientAlias.Organization, () => organizationAlias)
        .Left.JoinAlias(() => clientAlias.ClientStatusType, () => clientStatusTypeAlias)
        .Where(() => clientAlias.Id == clientId)
        .Future();

 var notes = session.QueryOver(() => clientAlias)
        .Left.JoinAlias(() => clientAlias.Notes, () => noteAlias)
        .Where(() => noteAlias.Client.Id == clientId)
        .Future();

var list = clientRootQuery.ToList();
        return list.FirstOrDefault();

它必须是左连接。

相关问题