views:

1074

answers:

0

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?