让nhibernate在多对多关系上执行连接

本文关键字:执行 连接 关系 nhibernate | 更新日期: 2023-09-27 17:54:37

我有以下情况,我卡住了。我有一个名为ContactAssociation的实体,它具有(除其他外)属性Client和Contact。我必须为满足某些条件的客户获得所有联系人。条件是:每个客户端都有属性BusinessEntities,它是BusinessEntity的集合,每个BusinessEntity都有一个BusinessLevel。这是代码,我相信会使它更容易理解:

link.Session.QueryOver<ContactAssociation>(() => ca)
                                       //.Fetch(asoc => asoc.Client)
                                       .JoinAlias(() => ca.Client, ()=> client)
.Left.JoinQueryOver<BuEntry>(() => client.BuEntries, () => be)
                                       .Where(() => client.ID == clientKey)
                                       .Where(() =>    be.BuLevel.LevelNo > buLevel);

我写了这样的东西我不断得到一个错误属性BuLevel。找不到LevelNo但是它在模型中

我尝试了标准,但我看到了同样的错误,只是它在NHProfiler而不是Visual Studio中,并且在Visual Studio中有一个更神秘的错误。无论如何……我最终用sql查询解决了这个问题,但是我应该如何编写QueryOver和/或Criteria来获得适当的结果?

提前谢谢你

公立小学这是我的SQL查询:
 careTeamMembers = link.Session.CreateSQLQuery(@"select c.* from ContactAssociation ca
                                inner join Contact c
                                on ca.contactkey = c.contactkey
                                left join ContactBu cb
                                on cb.contactkey = c.contactkey
                                left join BuEntry be
                                on cb.entrykey = be.entrykey
                                left join BuLevel bl
                                on be.levelkey = bl.levelkey
                                where ca.clientkey = :clientkey
                                and bl.levelno > :level")
            .AddEntity(typeof(Contact))
            .SetInt32("clientkey", clientKey)
            .SetInt32("level", buLevel)
            .SetMaxResults(1000)
            .List<Contact>();

让nhibernate在多对多关系上执行连接

BULevel上缺少一个join

link.Session.QueryOver<ContactAssociation>(() => ca)
                                       //.Fetch(asoc => asoc.Client)
                                       .JoinAlias(() => ca.Client, ()=> client)
.Left.JoinQueryOver<BuEntry>(() => client.BuEntries, () => be)
                                       .Where(() => client.ID == clientKey)
                                       .JoinQueryOver<BuLevel>(() => be.BuLevel)
                                       .Where(bu => bu.LevelNo > buLevel);