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.  [ 7 posts ] 
Author Message
 Post subject: Lazy Loading for non-collection nested objects?
PostPosted: Mon Nov 07, 2005 3:51 pm 
Newbie

Joined: Fri Nov 04, 2005 2:45 pm
Posts: 18
Location: Gainesville, FL
Sorry for the beginner's question.

From what I'm reading in the docs, it looks like lazy loading is not supported for non-collection nested objects?

Here's the situation:

I'm putting together a sample project, very simple, to evaluate NHibernate. I'm working from the Northwind DB, and I'm putting together a mapping for the Employees table. This table has a column, "ReportsTo", which is an FK to another row in the Employees table.

I've implemented this relationship in my mapping file with a <many-to-one /> association. This tag doesn't seem to have an option for lazy loading.

Am I missing something here? Is lazy load for a simple, non-collection relationship like this a bad idea?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 5:14 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
You have to add the lazy attribute to the class. So you would add lazy="true" to the Employee mapping.

I generally specify all of my entities as Lazy.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 12:14 pm 
Newbie

Joined: Fri Nov 04, 2005 2:45 pm
Posts: 18
Location: Gainesville, FL
Hmm. Having changed the mapping of the Employee class to include lazy="true", the objects produced by NHibernate now have null references for ReportsTo instead of the child Employee object. Here is my mapping:

Code:
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="NHibernateTest.BusinessObjects.Employee, NHibernateTest.BusinessObjects" table="Employees" lazy="true">
      <id name="Id" column="EmployeeID" type="Int32" access="nosetter.pascalcase-m-underscore">
         <generator class="native" />
      </id>

      <property name="LastName" type="string" />
      <property name="FirstName" type="string" />
      <property name="Title" type="string" />
      <property name="TitleOfCourtesy" type="string" />

      <property name="BirthDate" type="date" />

      <many-to-one name="ReportsTo" column="ReportsTo" />
   </class>
</hibernate-mapping>


Here is my class:

Code:
   public class Employee
   {
      private int m_Id;
      
      private string m_LastName;
      private string m_FirstName;

      private string m_Title;
      private string m_TitleOfCourtesy;

      private DateTime m_BirthDate;

      private Employee m_ReportsTo;

      public int Id
      {
         get { return this.m_Id; }
      }

      public string LastName
      {
         get { return this.m_LastName; }
         set { this.m_LastName = value; }
      }

      public string FirstName
      {
         get { return this.m_FirstName; }
         set { this.m_FirstName = value; }
      }

      public string Title
      {
         get { return this.m_Title; }
         set { this.m_Title = value; }
      }

      public string TitleOfCourtesy
      {
         get { return this.m_TitleOfCourtesy; }
         set { this.m_TitleOfCourtesy = value; }
      }

      public DateTime BirthDate
      {
         get { return this.m_BirthDate; }
         set { this.m_BirthDate = value; }
      }

      public Employee ReportsTo
      {
         get { return this.m_ReportsTo; }
         set { this.m_ReportsTo = value; }
      }
   }


This is how I am accessing the object:

Code:
Employee Test = ((Employee) BaseFactory.LoadObject(typeof(Employee), 1)).ReportsTo;


which results in an object whose members are all null.

What am I doing wrong here?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 1:38 pm 
Newbie

Joined: Fri Nov 04, 2005 2:45 pm
Posts: 18
Location: Gainesville, FL
Okay, I managed to figure out how to get lazy="true" working -- the properties of the Model class have to be marked virtual (or defined in an external interface) to allow the NHibernate bytecode-proxy generator to properly create the proxy classes.

However, I'm still having a problem, as the lazy-loading only seems to work for one level deep - i.e., the proxy classes don't seem to be implementing lazy load. Is this the case for (N)Hibernate normally? Is it impossible to lazy-load for more then one nested level?

Essentially, what I'd like to do here is this (referencing the above code):

Code:
Test.ReportsTo.ReportsTo.ReportsTo.LastName
or something similar.

Is this possible?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 3:48 pm 
Newbie

Joined: Fri Nov 04, 2005 2:45 pm
Posts: 18
Location: Gainesville, FL
Never mind, I'm retarded. After spending several hours working on this, I finally figured out that my DB's ReportTo columns weren't pointing to existing rows. Fixed that, and chaining works like a charm.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 6:47 am 
Beginner
Beginner

Joined: Mon Oct 03, 2005 4:59 am
Posts: 26
Location: Cambridge, UK
Thanks for posting your experiences with this. Lazy loading of many-to-one properties is something that I tentatively plan to do in the future, but my (admittedly brief) attempt to figure out the documentation on it was not successful. This thread will be quite helpful if I ever get around to it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 2:23 pm 
Newbie

Joined: Fri Nov 04, 2005 2:45 pm
Posts: 18
Location: Gainesville, FL
No problem. One thing to remember (one thing that I was doing wrong but forgot to mention) is that to enable lazy loading, the properties of the model object must either be defined in an interface, or be declared as virtual (both options are so that the reflection API can build the proxy class at runtime properly).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.