我试图从我的列表中获取数据,并使用Criteria包含来自GoogleAnalyticsTotals表的数据。代码可以工作,但执行的SQL会对Listing属性进行一次调用,然后对每个Listing的GoogleAnalyticsTotals数据进行多次调用。我试图设置它以便GoogleAnalyticsTotals数据在一次调用中返回列表数据。
我尝试过添加.CreateCriteria(" GoogleAnalyticsTotals",JoinType.InnerJoin);但所有这一切都是为第一个调用添加一个INNER JOIN,但不会从该表中检索字段。 SetFetchMode没有任何区别。
我已经尝试了很多版本的代码,我得出结论,问题是我使用的是Projections。当我从混合中取出Projections时,它可以作为一个调用。但我需要使用Projections,因为Listing对象有很多属性,我不想在这个调用中全部检索它们。这是我的标准代码:
ICriteria listingCriteria = this.CurrentNHibernateSession.CreateCriteria(typeof(Listing));
listingCriteria.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Id"), "Id")
.Add(Projections.Property("City"), "City")
.Add(Projections.Property("Company"), "Company")
.Add(Projections.Property("GoogleAnalyticsTotals"), "GoogleAnalyticsTotals")
.Add(Projections.Property("LandingPageTemplate"), "LandingPageTemplate")
.Add(Projections.Property("Title"), "Title"))
.SetFetchMode("GoogleAnalyticsTotals", FetchMode.Eager)
.SetResultTransformer(Transformers.AliasToBean(typeof(Listing)));
var list = listingCriteria.List();
return list.Cast<Listing>().ToList();
这是我的班级和地图档案:
public class Listing {
public virtual int Id { get; protected set; }
public virtual string City { get; set; }
public virtual Company Company { get; set; }
public virtual GoogleAnalyticsTotals GoogleAnalyticsTotals { get; set; }
public virtual LandingPageTemplate LandingPageTemplate { get; set; }
public virtual string Title { get; set; }
public Listing() {
}
}
public class GoogleAnalyticsTotals {
public virtual int Id { get; protected set; }
public virtual int Applys10Days { get; set; }
public virtual int Applys30Days { get; set; }
public virtual int ApplysTotal { get; set; }
public GoogleAnalyticsTotals() {
}
}
public class ListingMap : ClassMap<Listing> {
public ListingMap() {
Id(x => x.Id);
Map(x => x.City);
References(x => x.Company, "CompanyId");
References(x => x.GoogleAnalyticsTotals, "Id").Not.LazyLoad().Fetch.Join();
References(x => x.LandingPageTemplate, "LandingPageTemplateId");
Map(x => x.Title);
}
}
public class GoogleAnalyticsTotalsMap : ClassMap<GoogleAnalyticsTotals> {
public GoogleAnalyticsTotalsMap() {
Id(x => x.Id);
Map(x => x.Applys10Days);
Map(x => x.Applys30Days);
Map(x => x.ApplysTotal);
}
}
任何人都可以了解是否或如何使用Projections在多个表格中选择数据?
由于