NHibernate - 收集渴望加载 - 只有ids

时间:2014-11-04 21:19:50

标签: c# nhibernate collections eager-loading

我们说我有一个班级

public class Person
{
    public Person()
    {
        Friends = new Collection<Person>();
    }
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<Person> Friends { get; set; }

}

使用以下映射:

<class name="Person">
  <id name="Id" generator="guid"/>
  <property name="Name" />
  <set name="Friends">
    <key column="PersonId" foreign-key="none"/>
    <many-to-many class="Person" foreign-key="none"/>
  </set>
</class>

我试图吸引所有人并急切加载好友集合 ids
我不需要加载整个Friends集合,只需要加载朋友的ID 以下内容:

var persons = session.QueryOver<Person>()
                .Fetch(p => p.Friends).Eager
                .List<Person>();

生成这个sql语句:

SELECT this_.Id           as Id0_1_,
   this_.Name         as Name0_1_,
   friends2_.PersonId as PersonId3_,
   person3_.Id        as elt3_,
   person3_.Id        as Id0_0_,
   person3_.Name      as Name0_0_
FROM   Person this_
   left outer join Friends friends2_
     on this_.Id = friends2_.PersonId
   left outer join Person person3_
     on friends2_.elt = person3_.Id

这显然也吸引了所有的朋友实体 我尝试了很多组合,但无济于事 *我知道,因为它是同一个类,所以页面都会被加载。 我的问题是一般性的 - 只加载集合的ID。

0 个答案:

没有答案
相关问题