Edit: I solved the problem describe below using HQL and currently I am looking for answers to these questions:
1. Is there any way, how to apply extra join conditions using Criteria API? (The "with" keyword in HQL)
2. Is there any way, how to specify join order using parentheses in HQL / Criteria API?
I was not able to find this in the documentation or anywhere on the web and I wonder, why nobody has raised this question anywhere (though I found related problems on the forum, which have not been replied by anybody).
The problem is related to
left outer join.
Simple scenario. Parent(id) and Child(id, age). There is a relation (1..n) called "children" between Parent and Child. Parent(A) has no children, Parent(B) has three Children aged 10, 20 and 30.
Code:
Criteria parentCriteria = session.createCriteria(Parent.class);
Criteria childCriteria = parentCriteria.createCriteria("children", Criteria.LEFT_JOIN);
childCriteria.add(Restrictions.ge("age", new Long(15)));
...generates...
Code:
select parent.id, child.id, child.age
from Parent parent
left outer join Child child on parent.id=child.fk_parent_id
where child.age>=15;
I only get 2 results:
Parent(B)+Child(20)
Parent(B)+Child(30)
The result I need is what I need from a
left outer join:
Parent(A)+null
Parent(B)+Child(20)
Parent(B)+Child(30)
... in other words, I need this generated SQL query:
Code:
select parent.id, child.id, child.age
from Parent parent
left outer join Child child on parent.id=child.fk_parent_id and child.age >15
My questions:
1. How can I achieve the needed result? Something, that would show me, that I missed some part of the Hibernate technology. A workaround would be nice too (a only came with adding a isNull() restriction to the criteria).
.... if question 1 has no solution in Hibernate/Criteria API/HQL
2. What is the point of adding restrictions to SubCriteria object, when they are always applied to the same
where clause in the generated SQL. Is this only for the purpose of method chaining?
3. Does this mean, that the left outer join mode for fetching associations is useless once I apply a restriction to the resulting set ?