【发布时间】:2011-03-21 13:25:28
【问题描述】:
我已经远远超出了我的 NHibernate 能力,看来...
我正在尝试加载具有三个子集合的给定实体的 Top10,但 NHibernate 的 Linq 实现为我提供了正确的结果...以加载所有子集合的整个表为代价。
public class PartnerDto : BaseDto
{
private IList<EmailContactDto> _emailContacts;
private IList<TelephoneNumberDto> _telephoneNumbers;
private IList<string> _chains;
public EmailContactDto[] EmailContacts
{
get { return _emailContacts.ToArray(); }
set { _emailContacts = value.ToList(); }
}
public TelephoneNumberDto[] TelephoneNumbers
{
get { return _telephoneNumbers.ToArray(); }
set { _telephoneNumbers = value.ToList(); }
}
public string[] Chains
{
get { return _chains.ToArray(); }
set { _chains = value.ToList(); }
}
//more properties
}
映射:
<class name="PartnerDto" table="[Partner]" schema-action="none" lazy="false" mutable="false">
<id name="Id">
<generator class="hilo"/>
</id>
<bag name="Chains" table="PartnerChains" access="field.camelcase-underscore" lazy="false" fetch="subselect">
<key column="PartnerId"/>
<element column="Chain" type="System.String"/>
</bag>
<bag name="TelephoneNumbers" access="field.camelcase-underscore" lazy="false" fetch="subselect">
<key column="PartnerId"/>
<composite-element class="TelephoneNumberDto">
<property name="Name" not-null="true"/>
<property name="ClickToDial" not-null="true"/>
<property name="SMS" not-null="true"/>
<property name="Index" column="[Index]" not-null="true"/>
</composite-element>
</bag>
<bag name="EmailContacts" access="field.camelcase-underscore" lazy="false" fetch="subselect">
<key column="PartnerId"/>
<composite-element class="EmailContactDto">
<property name="Name" not-null="true"/>
<property name="Email" not-null="true"/>
<property name="NewsLetter" not-null="true"/>
<property name="Index" column="[Index]" not-null="true"/>
</composite-element>
</bag>
<!-- other properties -->
当我这样做时:
session.Query<PartnerDto>().Take(10);
我得到了正确的 10,但是我的 sql 显示它加载了 TelephoneNumber 表、EmailContact 表和 PartnerChains 表中的所有行,而不是将其限制为我正在加载的 10 个合作伙伴。
我错过了什么? (是的,我必须急切地加载所有内容 - 它们将在查询后立即序列化。)
编辑:找到解决方案:
我首先只查询分页查询的 ID,然后使用这些 ID 查询完整的对象图。由于前 10 个现在位于 where 子句中 - NHibernate 也使用它来过滤集合。是的,它并不完美 - 我总共有两次往返和五个查询,但目前仍然是我的最佳选择。
【问题讨论】:
标签: c# nhibernate linq-to-nhibernate