2

If I set LazyLoad on multiple properties-columns with NHiberante and access those properties one after the other, would it query the database for each property?
Example:

public class Product
{
    public virtual int ID {get; set;}
    public virtual string Name {get; set;}
    public virtual string FullName {get; set;}
    public virtual float Price {get; set;}
}

public class ProductMap : ClassMap<Product>
{
    Id(p => p.ID);
    Map(p => p.Name).Not.LazyLoad();
    Map(p => p.FullName).LazyLoad(); // Redundant - I know...
    Map(p => p.Price).LazyLoad();    // Redundant - I know...
}

if I query the DB like this:

var product = session.Load<Prodct>(2);
if (product.FullName == "*" && product.Price = 111)

Will there be 3 queries

  1. The Product entity
  2. The FullName property
  3. The Price property

or when NHibernate query the DB for FullName it will query all the columns of the row?

2 Answers 2

2

NHibernate will load all the lazy properties of an entity in a single query (you can try for yourself...)

The main use case for this feature is blobs.

Lazy references, on the other hand, are loaded as needed.

As a side note, session.Load does not query the DB; it just creates a proxy, which will be loaded lazily. Use session.Get.

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I see as well, so There will be two queries right? Hope this dissuction will be shorter then that are handling in other thread. ;-)
Yes, you'll have one query for the non-lazy properties, and one for all the lazy ones.
2

there will be 2 queries

  1. The Product entity
  2. All LazyLoaded Properties

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.