nHibernate复合连接

时间:2010-06-22 19:12:41

标签: c# nhibernate

我需要在表上执行复杂的左连接,并且不确定如何使用条件查询编写代码。目前我有:

 public IList<RezolutionConfig> GetSearchConfigByManagerCategoryProduct(int ManagerId, int ProductTypeId, int ConfigCategoryId)
    {
        ICriteria criteria = Session.GetISession().CreateCriteria(typeof(RezolutionConfig))
            .CreateAlias("RezolutionConfigCategory", "rcc")
            .Add(Expression.Eq("rcc.id", ConfigCategoryId))
            .CreateAlias("RezolutionProductType","rpt")
            .Add(Expression.Eq("rpt.id", ProductTypeId))
            .CreateAlias("RezolutionManagerConfigs", "rmc", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
            .CreateAlias("rmc.Manager", "m", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
            .Add(Expression.Eq("m.id", ManagerId));

        return criteria.List<RezolutionConfig>();
    }

产生:

SELECT * FROM [dbo]。[RezolutionConfig] this_ 内连接[dbo]。[RezolutionConfigCategory] ​​rcc1_ on this _。[RezolutionConfigCategoryId] = rcc1 _。[RezolutionConfigCategoryId] 内连接[dbo]。[RezolutionProductType] rpt2_ on this _。[RezolutionProductTypeId] = rpt2 _。[RezolutionProductTypeId] 左外连接[dbo]。[RezolutionManagerConfig] rmc3_ on this _。[RezolutionConfigID] = rmc3 _。[RezolutionConfigID] left outer join [dbo]。[Manager] m4_ on rmc3 _。[ManagerID] = m4 _。[ManagerID] WHERE rcc1 _。[RezolutionConfigCategoryId] = 1 和rpt2 _。[RezolutionProductTypeId] = 1 和m4 _。[ManagerID] = 9135

我需要制作的是

SELECT * FROM [dbo]。[RezolutionConfig] this_ 内连接[dbo]。[RezolutionConfigCategory] ​​rcc1_ on this _。[RezolutionConfigCategoryId] = rcc1 _。[RezolutionConfigCategoryId] 内连接[dbo]。[RezolutionProductType] rpt2_ on this _。[RezolutionProductTypeId] = rpt2 _。[RezolutionProductTypeId] 左外连接[dbo]。[RezolutionManagerConfig] rmc3_ on this _。[RezolutionConfigID] = rmc3 _。[RezolutionConfigID] left outer join [dbo]。[Manager] m4_ on rmc3 _。[ManagerID] = m4 _。[ManagerID]和m4 _。[ManagerID] = 9135 在哪里rcc1 _。[RezolutionConfigCategoryId] = 1 和rpt2 _。[RezolutionProductTypeId] = 1

1 个答案:

答案 0 :(得分:0)

NHibernate不支持根据需要为连接添加约束:left outer join [dbo].[Manager] m4_ on rmc3_.[ManagerID]=m4_.[ManagerID] and m4_.[ManagerID] = 9135。我认为您最好的方法是在HQL中编写查询并使用子查询来约束Manager。