We have a problem where we need to load a deep object graph. Lazy loading produces thousand of individual sql statements, whereas eager fetch generates a cartesian product containing millions of rows. Both approaches are infeasible as they create serious performance problems. Really what we need is one select per table which will correctly hydrate the object graph.
What's the best approach for dealing with this problem?
The problem is not dissimilar to Mats Helander ORM challenge:
http://www.matshelander.com/wordpress/?p=55
The main difference is that we need to load the data for a single customer/user -- loading the data for all customers is infeasible as there are 100,000 customers in the database.
Another constraint is that we don't have bidirectional references in our domain model (ie. from Mats example, you can't get from an OrderLine back to an Order).
To be specific, our domain looks like this:
- A user has multiple contact lists
- Each contact list contains multiple contacts
- Each contact has multiple addresses, email addresses, telephone numbers, etc.
For a single user, we need to eagerly load all contact lists along with the all of the nested child entities. It is a simple matter to compose separate SQL statements for each table joining in the ids for the parent objects. What I'm not clear on is how to coax NHibernate to do this.
I've looked at MultiQuery, but I'm not clear on how to use it to solve this situation? Can anyone recommend the best approach? Our current solution is to bypass nHibernate entirely and write some mappers to wire up the object graph by hand. Hardly ideal.
We're using NHibernate 1.2 GA.