views:

331

answers:

1

Hi

Iam struggling with NHibernate and its lazyload.

I have a structure which I simplified but it show my issue.

Class Shift {
 int ShiftID;
 DateTime ShiftStart;
 Employee Employee;
}

Class Employee {
 int EmployeeID;
 string Name;
}

Data:

 ShiftData
 ID                         SHIFTTIME       EmployeeID (int)
 1                           12:00                  0
 2                           13:00                  1
 3                           14:00                  0                           
 4                           13:00                  3


Employees
ID                         NAME
1                           Morten
2                           Peter
3                           Henrik

My loading strategy for the shift is using Join so that when I load the shifts NHibernate automatically does a Left join to get the customer. This works perfectly for the shifts with Employees attached, however some shifts haven’t got any employees yet.

When I try to access the Employee of such a shift once the shift is loaded it results in another SELECT against the database (found out by using your NHProfiler) Why does this happen ?

Hope you have an answer iam really stuck on this.

+1  A: 

My guess is that this happens because shifts without employees will have an empty Employee proxy. NHibernate sees the empty proxy and dutifully tries to load the data from the database.

As for the solution, seeing your mapping would really help. However, you might try ensuring that the relationship has the fetch="join" attribute set or turning off lazy loading for this relationship.

Stuart Childs
Well i actually tried quite alot. Doing a Join, turning of Lazy Load on both the class and the Reference. public ShiftMap() { Not.LazyLoad(); WithTable("shift_db"); Id(x => x.id,"shift_id").WithUnsavedValue(0); Map(x => x.Starttime,"shift_start"); References(x => x.Employee,"shift_employee").Join().NotFound.Ignore().Not.LazyLoad(); }
Morten Schmidt