views:

3239

answers:

6

What is the difference between lazy="true" and lazy="proxy" in nhibernate?

+3  A: 

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.

Frederik Gheysels
A: 

There is some useful info in the hibernate documentation

Chris Ballard
The question was about NHibernate, the .NET library. Not Hibernate, the java library. Just a heads up.
Samuel Meacham
A: 

The documentation states that lazy is a boolean property, ie only the values true/false is valid. Are you perhaps thinking of the proxy attribute that lets you specify your own lazy proxy?

Simon Svensson
+3  A: 

The documentation reference says that the value of the proxy attribute is in:

lazy="proxy|no-proxy|false"

lazy (optional - defaults to proxy): By default, single point associations are proxied. lazy="no-proxy" specifies that the property should be fetched lazily when the instance variable is first accessed (requires build-time bytecode instrumentation). lazy="false" specifies that the association will always be eagerly fetched.

streetpc
Shouldn't lazy="proxy" specify that the property should be fetched lazily when the instance variable is first accessed
A: 

By default, Hibernate3 uses lazy select fetching for collections and lazy proxy fetching for single-valued associations. These defaults make sense for most associations in the majority of applications.

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

Ali Abdel-Aziz
+3  A: 

I suspect another way of thinking about it would be this.

class Foo
{
   public virtual Bar SingleBar { get; set; }
   public virtual ICollection<Bar> MultiBar { get; set; }
}
  • lazy="proxy" applies to single objects (ie foo.SingleBar)
  • lazy="true" applies to collections of objects (ie foo.MultiBar)

(You can't set lazy="proxy" to a collection, nor can you set lazy="true" to a single reference. Either will cause NH to throw a XmlSchemaException which is a little cryptic to beginners.)

Operationally they do the same abstract thing: when and only when the property is accessed does NHibernate hit the database and populate the property.

There is however a slight difference in the implementation due what is needed to fetch the objects (in the single case, the reference's id (Bar) was loaded with the parent entity (Foo). In the collection case, the ids are unknown and must be found in another table)

fostandy