How to join NHibernate QueryOver

I have the following situation in my code and I cannot solve it. Situation -

var grpA = Session.QueryOver(() => _employeeGroupRelation));
var grpB = Session.QueryOver(() => _employeeGroup));

// var grpC should join grpA and grpB on _employeeGroupRelation.groupID = _employeeGroup.ID 

      

Question - Is there a way to join grpA and grpB using QueryOver syntax? Is it possible to do this without using List () on grpA or grpB, because each of them will contain about 10,000 records and I don't want to dump them into memory. Correct use of QueryOver? Are there cleaner ways to achieve what I'm trying to solve?

Perhaps this is the main doubt, but I am new to NHib and QueryOver.

Edit -

select * from employeeGroup a
inner join employeeGroupRelation b on a.ID = b.ID 

      

This is what I am trying to do in SQL.

+3


source to share


1 answer


The easiest way to do it:

session.QueryOver<EmployeeGroup>()
   .JoinQueryOver(employeeGroup => employeeGroup.EmployeeGroupRelation)
   .Take(1000) // The same as top in SQL, if you don't want load all of entities
   .TransformUsing(Transformers.DistinctRootEntity)
   .List();

      

"JoinQueryOver" gets "EmployeeGroup" and associated "EmployeeGroupRelation" as proxy (mapping dependent) to LazyLoad



If you don't want to use LazyLoad, you can do this:

session.QueryOver<EmployeeGroup>()
   .Fetch(employeeGroup => employeeGroup.EmployeeGroupRelation).Eager
   .Take(1000) // The same as top in SQL, if you don't want load all of entities
   .TransformUsing(Transformers.DistinctRootEntity)
   .List();

      

"Fetch" gets "EmployeeGroup" and associated "EmployeeGroupRelation" (not proxy)

+8


source







All Articles