Fluent NHibernate映射CompositeId并使用SetProjection进行查询

时间:2009-07-13 19:45:11

标签: nhibernate fluent-nhibernate

我有两个表(Section和SectionList),它们由多对多表(Membership)相关联。由于多对多表中有一个额外的属性,我必须将其分解为自己的实体:

    public MembershipMap()
    {
        UseCompositeId()
            .WithKeyReference(x => x.Section, "SectionId")
            .WithKeyReference(x => x.SectionList, "SectionList"); 
        Map(x => x.Status);
    }

并且SectionList映射如下:

    public SectionListMap()
    {
        Id(x => x.Id)
            .WithUnsavedValue(0);

        HasMany(x => x.Memberships)
            .Inverse()
            .Cascade.AllDeleteOrphan();
    }

这种关系似乎工作正常,除了我尝试在其上运行高级查询。例如,这是一个只捕获某些字段并转换为DTO的查询:

    var criteria = DetachedCriteria.For<CustomSectionListMembership>()
            .CreateAlias("SectionList", "sl")
            .CreateAlias("Section", "s")
            .CreateAlias("s.Website", "w")
            .CreateAlias("w.Publisher", "p")
            .SetProjection(Projections.ProjectionList()
                .Add(Projections.Property("s.Id"), "SectionId") //add projections for every propery you want returned
                .Add(Projections.Property("s.Name"), "SectionName") // mapping entity name -> DTO name
                .Add(Projections.Property("w.Id"), "WebsiteId")
                .Add(Projections.Property("w.Name"), "WebsiteName")
                .Add(Projections.Property("p.Id"), "PublisherId")
                .Add(Projections.Property("p.Name"), "PublisherName")
                .Add(Projections.Property("Status")))
            .Add(Expression.Eq("sl.Id", listId))
            .SetResultTransformer(Transformers.AliasToBean(typeof(MembershipDTO))); //transform to the DTO
    var membership = repository.FindAll(criteria);

此查询错误“无法执行查询”,因为生成的查询完全缺少应由CreateAlias调用生成的内部联接:

SELECT s2_.SectionId               as y0_,
   s2_.Name                    as y1_,
   w3_.WebsiteId               as y2_,
   w3_.Name                    as y3_,
   p4_.PublisherId             as y4_,
   p4_.Name                    as y5_,
   this_.Status as y6_ FROM Membership this_ WHERE csl1_.ListId = 6923 /* @p0 */

可能是什么问题?

0 个答案:

没有答案