nhibernate - 复杂对象的 Fluent NHibernate N+1 问题

标签 nhibernate fluent-nhibernate nhibernate-mapping

我在使用 NHibernate 多次查询数据库时遇到问题。我刚刚意识到它可能与 n+1 问题有关,但我无法弄清楚如何更改我的映射来解决问题。

正如您将看到的,我的尝试涉及指定不延迟加载其他对象,但它似乎并没有做到这一点。

这是查询:

public IQueryable<Report> ReadAll(DateTime since)
{
    return m_session.QueryOver<Report>()
       .JoinQueryOver(r => r.Mail)
       .Where(m => m.Received >= since)
       .List()
       .AsQueryable();
}

提前感谢您的任何回复!如果您需要有关我的对象或映射的更多信息,请告诉我。

简化的对象图(部分省略):
public class Report : EntityBase
{
    public virtual Product Product { get; set; }
    public virtual StackTrace StackTrace { get; set; }
    public virtual Mail Mail { get; set; }

    public virtual IList<ClientUser> ReadBy { get; set; }
}

——
public class Product : EntityBase
{
    public virtual string Name { get; set; }
    public virtual Version Version { get; set; }
    public virtual IList<Report> Reports { get; set; }
    public virtual IList<StackTrace> StackTraces { get; set; }
}

——
public class StackTrace : EntityBase
{
    public virtual IList<StackTraceEntry> Entries { get; set; }
    public virtual IList<Report> Reports { get; set; }
    public virtual Product Product { get; set; }
}

映射示例:
public class ReportMap : ClassMap<Report>
{
    public ReportMap()
    {
        Table("Report");

        References(x => x.User)
          .Column("EndUserId")
          .Not.LazyLoad();

        References(x => x.Product)
          .Column("ProductId")
          .Not.LazyLoad();

        References(x => x.StackTrace)
          .Column("StackTraceId")
          .Not.LazyLoad();

        HasManyToMany(x => x.ReadBy)
          .Cascade.SaveUpdate()
          .Table("ClientUserRead")
          .ParentKeyColumn("ReportId")
          .ChildKeyColumn("ClientUserId")
          .Not.LazyLoad().BatchSize(200);
    }
}

——
public class StackTraceMap : ClassMap<StackTrace>
{
    public StackTraceMap()
    {
        Table("StackTrace");

        References(x => x.Product)
          .Column("ProductId");

        HasMany(x => x.Entries)
          .KeyColumn("StackTraceId")
          .Not.LazyLoad()
          .Cascade
          .All().BatchSize(500);

        HasMany(x => x.Reports)
          .KeyColumn("StackTraceId")
          .Inverse().BatchSize(100);
    }
}

最佳答案

要走的路是使用 batch fetching .在此处阅读更多相关信息:

How to Eager Load Associations without duplication in NHibernate?

在每个实体映射上应用 BatchSize (对于 many-to-one 关系 - 避免 1 + N)

public ReportMap()
{
   Table(...)
   BatchSize(25);
   ...

并且在每个集合上(使用 one-to-many 1 + N 解决问题)
HasMany(x => x.Reports)
      .KeyColumn("StackTraceId")
      .BatchSize(25)
      ...

关于nhibernate - 复杂对象的 Fluent NHibernate N+1 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39262983/

相关文章:

c# - 如何使用 Fluent NHibernate 和多个数据库识别特定实体的 session 工厂

c# - 使用 NHibernate 将行插入数据库表而不在插入行后调用 scope_identity

nhibernate - 如何从FluentNHibernate生成hbm.xml文件

NHibernate 执行无关的选择语句

nhibernate - NHibernate SQLite编码

c# - 使用方法在 LINQ 中投影嵌套 DTO

.net - 引用的 DLL 未复制到引用项目

c# - N-Hibernate 中的长字符串与 Oracle 导致错误

wcf - 流利的 hibernate 循环引用疼痛

asp.net-mvc - 实体的 nhibernate : a different object with the same identifier value was already associated with the session: 2,: