Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Criteria and multiple join

is possible with Hibernate criteria do it?

select A.something, B.something, C.something, D.something
    from  A JOIN B on A.id = B.id_fk
          JOIN C ON B.id = C.id_fk
          JOIN D ON C.id = D.id_fk;
like image 544
Andrea Scarafoni Avatar asked Feb 12 '10 14:02

Andrea Scarafoni


1 Answers

I have got exactly the same problem, and was able to resolve it like this:

return criteria.createCriteria(A.class)
               .createCriteria("b", "join_between_a_b")
               .createCriteria("c", "join_between_b_c")
               .createCriteria("d", "join_between_c_d")
               .add(Restrictions.eq("some_field_of_D", someValue));

Note: "b", "c" and "d" in code above refer to attribute names in A, B and C classes, correspondingly (class A has attribute b and so on).

For this solution you don't even need to have lazy and fetch parameters to be set in your A.hbm.xml.

like image 54
mindas Avatar answered Sep 23 '22 13:09

mindas