-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Loading not quite as lazy as expected?
PostPosted: Mon Jun 07, 2010 4:17 pm 
Newbie

Joined: Mon Jun 07, 2010 3:07 pm
Posts: 4
Unfortunately I am new to NHibernate and therefore easily confused. Hopefully someone with more experience can set me straight here.

My understanding of lazy loading is that data only gets loaded when it is requested. That's not exactly what I am seeing in my demo project and I don't understand why.

I have a Person object:
Code:
  public partial class Person
    {
        ....

        List<RotationEvent> rotationEvents = new List<RotationEvent>();
        public virtual IList<RotationEvent> RotationEvents
        {
            get { return rotationEvents; }
            set { rotationEvent = value.ToList(); }
        }
    }


The relevant part of the mapping class looks like this
Code:
  public class PersonMap : ClassMap<Person>
    {
        public PersonMap()
        {
            Id(x => x.UniqueID);
            HasMany(x => x.RotationEvents).LazyLoad().Cascade.All();
            ...
        }
    }


All of this works in terms of saving and retrieving data from the database.

I mainly use the Person object to initialize a PersonDTO object, which contains less data. In particular, it doesn't contain the IList of RotationEvents. However, I see a database call to the RotationEvent table in the log file for each Person object when I initialize the PersonDTO object. This makes data access very slow, because there are hundreds of Person objects.

I was expecting to see no calls to the RotationEvent table at all, since initializing the PersonDTO object doesn't access the RotatonEvents property of the Person object (verified by placing a breakpoint on the get).

Am I misunderstanding how lazy loading is supposed to work or am I doing something that forces NHibernate to load the RotationEvents even though I don't use them in this context?

Thank you!


Top
 Profile  
 
 Post subject: Re: Loading not quite as lazy as expected?
PostPosted: Tue Jun 08, 2010 9:35 pm 
Newbie

Joined: Mon Jun 07, 2010 3:07 pm
Posts: 4
I think I discovered a way to prevent unnecessary loads from happening, but I can't say that I fully understand why the fix fixes anything. Two factors apparently contributed to causing NH to eager load everything:

1. I had a foreach loop over the Person collection to populate the PersonDTO objects. Looping through the enumeration caused all properties of the Person object to be loaded.

2. replacing the list of RotatonEvents in the Person object as follows eliminated the remaining eager loading:

Code:
public partial class Person
    {
        ....
        public virtual IList<RotationEvent> RotationEvents {get; set;}
    }


The first one makes some level of sense to me, but the second one is perplexing. I'd be interested, if anyone can shed some light on why this makes a difference.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.