29

nhibernatelazy="true"和in有什么区别?lazy="proxy"

4

4 回答 4

24

我怀疑另一种思考方式是这样的。

class Foo
{
   public virtual Bar SingleBar { get; set; }
   public virtual ICollection<Bar> MultiBar { get; set; }
}
  • lazy="proxy" 适用于单个对象(即 foo.SingleBar)
  • lazy="true" 适用于对象集合(即 foo.MultiBar)

(您不能将lazy="proxy" 设置为集合,也不能将lazy="true" 设置为单个引用。两者都会导致NH 抛出XmlSchemaException,这对初学者来说有点神秘。)

从操作上讲,它们执行相同的抽象操作:当且仅当访问属性时,NHibernate 才会访问数据库并填充属性。

然而,由于获取对象需要什么(在单一情况下,引用的 id (Bar) 与父实体 (Foo) 一起加载。在收集情况下,id 是未知的并且必须在另一个表中找到)

于 2009-11-24T18:18:09.150 回答
19

lazy="proxy" means that NHibernate will lazily initialize instances of your class; When NHibernate retrieves an instance of your class from the database, it will - in this case - not return a 'real' instance of your class, but it will rather give you a proxy. That is, it will return an object that is of another type, more specifically, an object that is a subclass of your class (generated by NHibernate through IL generation).

The object that you will be given, is a proxy, and the only populated property, is the Id property. As soon as you call another property on the instance, NHibernate will initialize the proxy, and retrieve all the other properties / collections (except those that are lazy loaded) from the database.

Lazy="true" is used on another level. Whereas lazy="proxy" is used on the class-level, lazy="true" is used on the collection level. It means that the collection should be lazy loaded.

于 2009-06-22T12:28:44.657 回答
10

文档参考说代理属性的值在:

lazy="proxy|no-proxy|false"

懒惰(可选 - 默认为代理):默认情况下,单点关联是代理的。

lazy="no-proxy" 指定第一次访问实例变量时应该延迟获取属性(需要构建时字节码检测)。

lazy="false" 指定总是急切地获取关联。

于 2009-06-22T12:54:03.483 回答
2

默认情况下,Hibernate3 对集合使用惰性选择获取,对单值关联使用惰性代理获取。这些默认值对于大多数应用程序中的大多数关联都是有意义的。

http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#performance-fetching-lazy

于 2009-10-21T12:38:42.500 回答