NHibernate是否使用对象实习?

| 为什么此代码有效? NHibernate是否使用对象实习? 如果不是,则以下方法可行,因为NHibernate重载等于运算符?
foreach (var c in s.Query<Country>())
{
    Console.WriteLine(\"\\n{0}\", c.CountryName);

    // code in question 
    foreach(var p in s.Query<Person>().Where(x => x.Country == c) )
        Console.WriteLine(\"{0}\", p.PersonName);

}
    
已邀请:
        不能肯定地说出NHibernate,但是如果它遵循与Hibernate相同的规则(据我怀疑),则可以保证在给定的会话中,给定ID只能有一个给定实体的实例。这意味着,如果会话中已经加载了Country实例,并且某个人的国家/地区具有此加载的实例的ID,则该国家/地区和该人的国家/地区将是同一实例,这是很合逻辑的。 会话是一个缓存,此缓存中只有一个具有给定ID的实体实例。无需操作员重载。     
        我猜可能会得出一个“假”结论,即如果使用Linq,则Hibernate正在使用对象Interning。由于这也起作用(即产生行):
foreach (var c in s.Query<Country>())
{
    Console.WriteLine(\"\\n{0}\'s people\", c.CountryName);

    // different memory copy
    var cx = new Country { CountryId = 1 };


    foreach(var p in s.Query<Person>().Where(x => x.Country == cx) )
        Console.WriteLine(\"{0}\", p.PersonName);
}
NHibernate的Linq-to-db提供程序在比较对象时并不关心实体的其他字段(实际上,事物是在DB级别上进行比较,而不是对象),它仅比较id。因此,上述代码的Linq-to-db只是将内容转换为:WHERE CountryId = 1。 而如果我们热切地将对象提取到内存中,则可以更轻松地推断行为。这不会产生任何行,即使我们复制所有属性也是如此,因为cx指向不同的地址。因此,此Linq-to-memory不产生任何行:
foreach (var c in s.Query<Country>())
{
    Console.WriteLine(\"\\n{0}\'s people\", c.CountryName);

    var cx = new Country
    {
         CountryId = c.CountryId,
         CountryName = c.CountryName,
         People = c.People.ToArray().ToList(),
         Population = c.Population
    };

    foreach(var p in s.Query<Person>().Fetch(x => x.Country)
                      .ToList().Where(x => x.Country == cx) )
    {
        Console.WriteLine(\"{0}\", p.PersonName);
    }

}
这是另一个Linq-to-memory代码,这一次它生成行,因此我们可以得出结论NHibernate实习生对象。
var china = s.Get<Country>(1);
china.Population = 777;

foreach (var c in s.Query<Country>())
{
    Console.WriteLine(\"\\n{0}\'s people\", c.CountryName);

    foreach(var p in s.Query<Person>().Fetch(x => x.Country)
                      .ToList().Where(x => x.Country == china) )
    {   
        Console.WriteLine(\"{0} {1}\", p.PersonName, p.Country.Population);
    }
}
除了上面的代码会产生行外,Country china与Person \'s Country共享同一存储位置的另一个证明是:Person.Country.Population也输出777。 所以我想我可以得出结论,NHibernate也使用对象实习(在实现方面,我认为NH有效地实现了事情,在决定使用内部对象时不需要比较所有属性,可以将ID用作一种机制, whatdya想?:-))     

要回复问题请先登录注册