Nhibernate Left Outer Join返回Join的第一个记录

时间:2010-05-24 08:45:12

标签: nhibernate criteria left-join

我有以下映射,其中我试图使用左连接恢复与产品关联的0 - 1媒体ID(我讨厌包括我的尝试,因为它混淆了情况)

ICriteria productCriteria = Session.CreateCriteria(typeof(Product));

productCriteria  
.CreateAlias("ProductCategories", "pc", JoinType.InnerJoin)  
.CreateAlias("pc.ParentCategory", "category")  
.CreateAlias("category.ParentCategory", "group")  
.Add(Restrictions.Eq("group.Id", 333))  
.SetProjection(  
    Projections.Distinct(  
        Projections.ProjectionList()  
            .Add(Projections.Alias(Projections.Property("Id"), "Id"))  
            .Add(Projections.Alias(Projections.Property("Title"), "Title"))  
            .Add(Projections.Alias(Projections.Property("Price"), "Price"))  
            .Add(Projections.Alias(Projections.Property("media.Id"), "SearchResultMediaId")) // I NEED THIS  
    )  
)  
.SetResultTransformer(Transformers.AliasToBean<Product>());  

IList<Product> products = productCriteria  
.SetFirstResult(0)  
.SetMaxResults(10)  
.List<Product>();  

我需要使用Media.Id填充SearchResultMediaId的查询,我只想在左外连接中恢复第一个Media,因为这是产品和媒体之间的1对多关联

产品按以下方式映射到媒体

mapping.HasManyToMany<Media>(x => x.Medias)  
.Table("ProductMedias")  
.ParentKeyColumn("ProductId")  
.ChildKeyColumn("MediaId")  
.Cascade.AllDeleteOrphan()  
.LazyLoad()  
.AsBag();  

任何帮助都会很棒。

0 个答案:

没有答案
相关问题