NHibernate - LazyLoad one to zero

Iam is struggling with NHibernate and its lazy load.

I have a structure that I have simplified, but it shows my problem.

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 transition is to use Join so that when the offsets are loaded, NHibernate will automatically join Left to get the client. This works great for shifts with attached employees, however some of the changes haven't received any employees yet.

When I try to access the Worker of such a shift after loading the shift, it results in another SELECT to the database (detected by your NHProfiler) Why is this happening?

I hope you have an answer that is really stuck on this one.

+1


a source to share


1 answer


I assume this is because shifts without employees will have an empty proxy Employee

. NHibernate sees an empty proxy and dutifully tries to load data from the database.



As for the solution, your mapping will really help. However, you can try to make sure the relationship has a fetch = "join" attribute set or disables lazy loading for that relationship.

+1


a source







All Articles