Вы можете использовать размер пакета для предварительной выборки экземпляров, что значительно сокращает количество запросов.
отображение (не уверено, поддерживается ли оно пока Fluent):
HasMany(x => x.ChildComponentAttributes)
.Fetch.Select()
.SetAttribute("batch-size", "20")
.Inverse()
.Cascade.AllDeleteOrphan()
.KeyColumn("ParentComponentAttributeId");
Если у вас есть свойство Root, вы можете сделать запрос для всего дерева сразу.
public class ComponentAttributeDto
{
public virtual ComponentAttributeDto ParentComponentAttributeDto { get; private set; }
public virtual ComponentAttributeDto Root
{
get
{
if (ParentComponentAttributeDto == null)
{
return this;
}
else
{
return ParentComponentAttributeDto.Root;
}
}
private set
{ /* just for NH to call it */ }
}
// ....
}
HasMany (x => x.Children) .AsSet (). SetAttribute ("batch-size "," 20 ")
запрос
session.CreateQuery(
@"from ComponentAttributeDto
where Root = :root"
.SetEntity(root);
должен фактически приводить только к одному запросу.Не уверен, что NH на самом деле не выполняет запросы для списков (ChildComponentAttributes), но стоит попробовать.