Search code examples
nhibernatehqllinq-to-nhibernateicriteriaqueryover

Multi-table NHibernate query


I have an object graph that looks like this:

class A ()
{
   int id;
   IEnumerable<B> bees;
}

class B()
{
   int id;
   A a;
   C c;
}

class C()
{
   int id;
   D d;
   IEnumerable<B> bees;
}

class D()
{
   int id;
   IEnumerable<C> cees
}

What would be a sensible approach for constructing a query which:

returns a list of A types, where they contain D down the chain with a particular id?

I'm using NH3, so can use any of the query techs. I've tried a couple of different approaches, but have hit seemingly dead ends in each case. I have a solution working with in memory collections - but obviously this is not ideal, I want the work done on the DB server.


Solution

  • select distinct a from A a
    inner join a.bees b
    inner join b.c c
    where c.d.id = :theSearchDId
    

    That is assuming that all the associations are bidirectional associations, and not unrelated associations (i.e. A.bees is the inverse association of B.a, etc.).