我如何避免一个“错误”;N+;1选择“;并为具有多对一的(N)Hibernate用户类型指定获取策略?
我有一个复合组件,它有一个多对一引用我如何避免一个“错误”;N+;1选择“;并为具有多对一的(N)Hibernate用户类型指定获取策略?,hibernate,nhibernate,nhibernate-mapping,Hibernate,Nhibernate,Nhibernate Mapping,我有一个复合组件,它有一个多对一引用 class MyComposite { SomeEntity ManyToOne { get; set; } SomeOtherUserType Value { get; set; } } 为了便于映射,我制作了一个自定义的ICompositeUserType,其中包含以下组件: class MyCompositeUserType : ICompositeUserType { // ... private static re
class MyComposite
{
SomeEntity ManyToOne { get; set; }
SomeOtherUserType Value { get; set; }
}
为了便于映射,我制作了一个自定义的ICompositeUserType
,其中包含以下组件:
class MyCompositeUserType : ICompositeUserType
{
// ...
private static readonly IType[] _propertyTypes = new[]
{
new ManyToOneType("SomeEntity"),
new CustomType(typeof(SomeOtherUserType))
};
// ...
}
现在,我有一个复合元素的集合
s,其中包括以下组件:
...
当我在容器
类上查询时,配对
集会按照设计被急切地加载,然而,我随后在SomeEntity
上得到一个N+1选择,它构成了MyCompositeUserType
的一部分。我想在配对
集合上加载这些实体以及连接
如何指定这一点?如果映射为组件,则可以使用lazy=“false”将其尽可能多地映射到一个组件,或者在查询时可以设置fetchmode
<composite-element class="Pair">
<component name="Item1">
<many-to-one name="Entity1" column="Entity1Id" />
<property name="Amount" column="Amount1" />
</component>
<component name="Item2">
<many-to-one name="Entity2" column="Entity2Id"/>
<property name="Amount" column="Amount2" />
</component>
<property name="Tag" column="Tag" />
</composite-element>
您使用什么代码进行查询?您是否指定要加载相关实体的提取?如果是这样,那么您是否使用转换器来删除重复的根元素?在查询中没有额外的回迁。这只是一个严格的列表。您需要ICompositeUserType
而不是
映射,这有什么原因吗?是的,它使映射许多使用它的实例变得更容易,并且生成的映射文档更简洁。@Firo-您是说不使用用户类型映射会让我可以选择指定获取类型吗?
<composite-element class="Pair">
<component name="Item1">
<many-to-one name="Entity1" column="Entity1Id" />
<property name="Amount" column="Amount1" />
</component>
<component name="Item2">
<many-to-one name="Entity2" column="Entity2Id"/>
<property name="Amount" column="Amount2" />
</component>
<property name="Tag" column="Tag" />
</composite-element>