Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nhibernate, eager loading and paging

I am creating an mvc application that uses nhibernate and paging. I have a parent > child relationship that I am trying to eager load my child records. This is all working fine.

The problem I am having is with the paging. I would like to have 15 items per page. This works perfectly if each parent has only one child. The problem is when a parent has more than 1 child. For instance, if the parent has 2 child records, then the database actually selects 15 records with two representing the same parent, one for each of the two children. Therefore, in my data view on the page, it appears that there are only 14 records.

Does anyone know how I might go about getting my page counts by parent only while still eager loading my child entities?
This is going to be a public facing site, so I don't think it would be a good idea to lazy load since it will cause too many trips to the server.

Is there something built into NHibernate that might handle this that I am missing?

Thanks for any thoughts.

like image 475
czuroski Avatar asked Dec 10 '10 14:12

czuroski


1 Answers

You could mark your association property with fetch="subselect" - this also makes sure you don't run into problems with a huge cartesian product at the cost of making two queries for each select.

like image 163
Goblin Avatar answered Nov 16 '22 02:11

Goblin