【问题标题】:NHibernate Join and Restriction CriteriaNHibernate 加入和限制条件
【发布时间】:2011-01-10 00:50:21
【问题描述】:

我正在尝试编写一个同时有效连接和限制的 NHibernate 标准。我的数据库看起来像这样...

案例 ---> CustomerProducts 案例状态

每个案例都与客户产品相关联(多个案例对应一个产品)。

每个客户都有一些客户产品(一个客户有很多客户产品)。

每个案例还有一个案例状态(一个案例状态到多个案例)。

这已使用 XML 文件进行映射,案例和客户(通过客户产品)之间的多对多解决方案已通过 CustomerProduct 映射中的 Sets 解决,这意味着 CustomerProduct 实体具有集合:

客户

案例

然后我创建一个键入“案例”的标准。我需要申请的标准是......

  1. 状态 IN [各种状态代码]。这已经实现了......

    criteria.Add(Restrictions.In("CaseStatus.CaseStatusId", statuses));

  2. 现在我只需要为特定客户 ID 选择案例。我试过了……

    criteria.Add("CustomerProduct.Customer.CustomerId", customerId);

这不起作用,NHibernate 告诉我它无法解析到 CustomerProduct.Customer.CustomerId 的映射。

Case 具有 CustomerProduct 对象的属性。

CustomerProduct 具有 Customer 对象的属性。

客户具有 CustomerId 的属性。

任何想法为什么它不起作用?

谢谢。

【问题讨论】:

    标签: nhibernate join criteria restriction


    【解决方案1】:

    我认为您需要为 CustomerProduct.Customer 创建一个别名:

    criteria.CreateAlias("CustomerProduct", "customerProduct");
    criteria.CreateAlias("customerProduct.Customer", "customer");
    criteria.Add(Restrictions.Eq("customer.CustomerId", customerId));
    

    我很惊讶 IN 限制在没有别名的情况下起作用。这是一个good article,关于使用 Criteria API 执行连接查询。

    【讨论】:

    • 完美运行!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2010-12-20
    • 1970-01-01
    • 2011-11-10
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多