NHibernate CreateAlias和Restrictions不会过滤集合

时间:2011-04-18 15:13:10

标签: nhibernate

我坚持这个并且找不到出路:/

我正在尝试做这样的事情(T-SQL)。在MS SQL Managment Studio中完美运行

select *
from Product join Price on (Product.ID = Price.ProductID)
where Product.ID = '15' and Price.ShopID = '20'

我已经像这样创建了Criteria

Product p = session.CreateCriteria<Product>("product")
                                  .Add(Restrictions.IdEq(productId))
                                  .CreateAlias("prices", "productPrice")
                                  .Add(Restrictions.Eq("productPrice.shop.id", shopId))
                                  .UniqueResult<Product>();

我在执行高级代码时得到了这个。看起来很不一样吧?但是当我调试并检查返回的产品(这里是'p')时,我得到了与之相关的所有价格:/不仅是一个有价值ShopId = '20'的价格。我保证在数据库中只有这种类型。

NHibernate: SELECT this_.id as id3_1_, this_.name as name3_1_, this_.description
 as descript3_3_1_, productpri1_.id as id2_0_, productpri1_.productPrice as prod
uctP2_2_0_, productpri1_.ProductID as ProductID2_0_, productpri1_.ShopID as Shop
ID2_0_ FROM [Product] this_ inner join [Price] productpri1_ on this_.id=productp
ri1_.ProductID WHERE this_.id = @p0 and productpri1_.ShopID = @p1;@p0 = 15 [Type
: Int32 (0)], @p1 = 20 [Type: Int32 (0)]

有人能指出我在这里做错了吗?

1 个答案:

答案 0 :(得分:1)

这是设计的。您无法使用限制来过滤集合。

如果你仔细观察,NHibernate将会两次访问数据库:一次是查询,一次是收集。