NHibernate加入和限制标准

时间:2010-01-27 15:47:14

标签: nhibernate join criteria restriction

我试图编写一个NHibernate标准,同时有效地加入和限制。我的数据库看起来像这样......

案例--->客户产品< ---客户 案例---> CaseStatuses

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

每个客户都有许多客户产品(一个客户拥有许多客户产品)。

每个案件还有一个案件状态(一个案件状态对很多案件)。

这已经与XML文件映射,并且案例和客户(通过客户产品)之间的多对多分辨率已经通过集合<>来解决。在CustomerProduct映射中,表示CustomerProduct实体具有以下集:

客户

然后我创建一个键入“Cases”的条件。我需要适用的标准是....

  1. 状态IN [varioud status codes]。这已经实现了......

    criteria.Add(Restrictions.In(“CaseStatus.CaseStatusId”,status));

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

    criteria.Add(“CustomerProduct.Customer.CustomerId”,customerId);

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

    Case具有CustomerProduct对象的属性。

    CustomerProduct具有Customer对象的属性。

    客户拥有CustomerId的财产。

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

    感谢。

1 个答案:

答案 0 :(得分:4)

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

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

我很惊讶IN限制没有别名。以下是关于使用Criteria API执行连接查询的good article

相关问题