简体   繁体   English

NHibernate选择N + 1和递归

[英]NHibernate Select N+1 and Recursive

I've got a couple of data classes: 我有几个数据类:

public class RecordGroup
{
    public virtual DataRecord RootDataRecord;
}

public class DataRecord 
{
    public virtual string Name { get; set; }
    public virtual RecordGroup RecordGroup { get; set; }
    public virtual IList<DataRecord> Children { get; set; }
    public virtual DataRecord Parent { get; set; }
    public virtual IList<DataProperty> DataProperties { get; set; }
    public virtual IList<Foto> Fotos { get; set; }
}

public class DataProperty
{
    public virtual string Name { get; set; }
    public virtual string Value { get; set; }
    public virtual IList<Foto> Fotos { get; set; }
}

public class Foto
{
    public virtual string Name { get; set; }
    public virtual byte[] Data { get; set; }
}

So 1 RecordGroup is "connected" to several DataRecords, having several children (which again got children etc.) each of them having several Properties and Fotos. 因此,一个RecordGroup被“连接”到多个DataRecords,有几个子级(又有子级等),每个子级都有几个Properties和Fotos。 I need all the DataRecords including Children, Properties and Fotos according to a certain RecordGroup. 我需要根据某个RecordGroup的所有DataRecords,包括Children,Properties和Fotos。

Doing this within raw SQL is a simple statement with a few joins, but when i try to do this with linq and nhibernate it results in 1500 Select N+1 Statements, and an enormous slowdown. 在原始SQL中执行此操作是一个简单的语句,其中包含一些联接,但是当我尝试使用linq和nhibernate进行此操作时,它导致1500个Select N + 1语句,并且运行速度非常慢。

I already tried .FetchMany( x => x.Children ); 我已经尝试过.FetchMany( x => x.Children );

How is it possible to get the whole "datatree" of 1 Recordgroup within 1 Query? 如何在1个查询中获得1个记录组的整个“数据树”?

Thanks in advance!!!! 提前致谢!!!!

I think, you need something like this: 我认为,您需要这样的东西:

   var recordGroupId = // your recordGroup Id
   Session.QueryOver<DataRecord>()
     .Where(dataRecord.RecordGroup.Id == recordGroupId)
     .Fetch(dataRecord  => dataRecord.Children).Eager
     .Fetch(dataRecord  => dataRecord.DataProperties).Eager
     .Fetch(dataRecord  => dataRecord.Fotos).Eager
     .TransformUsing(Transformers.DistinctRootEntity)
     .List<DataRecord>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM