Fluent NHibernate mapping a CompositeId and Querying with SetProjection

不羁的心 提交于 2019-12-08 00:27:32

问题


I have two tables (Section and SectionList) that are related by a many to many table (Membership). Since there is an extra property in the many to many table, i have to break it out into its own entity:

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

And SectionList is mapped as follows:

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

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

The relationship seems to work fine, except for when I try to run advanced queries on it. For instance, here is a query that grabs only certain fields and transforms to a 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);

This query errors out with "could not execute query", because the query being generated is completely missing the inner joins that should have been generated by the CreateAlias calls:

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 */

What could possibly be the problem?

来源:https://stackoverflow.com/questions/1121651/fluent-nhibernate-mapping-a-compositeid-and-querying-with-setprojection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!