fydoken wrote:
That's a great idea. In my tests, the results turn out the same with left join fetch or inner join fetch. Would upgrading to 3.0 help this situation?
The results
should be the same in this case. left join in HQL has the same meaning as in SQL -- include all data in the class (table) on the left, regardless whether there is a matching object on the right. Inner join means return rows only if there is a match on both sides. In your case,
Code:
from com.Child child
left join fetch child.parent as parent
where child.id = ?
should have equivelent results to inner join because there should never be a child with no parent. If the query was:
Code:
from com.Parent parent
left join fetch parent.children
where parent.id = ?
Your query would return the Parent object and its children, if any. However,
Code:
from com.Parent parent
inner join fetch parent.children
where parent.id = ?
would retrieve the parent object (and its children) only if it
has children.
Back to your query, though. Since you want the child and it's parent, and you know the parent is there, the difference is in the generated SQL. An HQL join becomes the same type in the generated SQL. So,
Code:
from com.Child child
left join fetch child.parent as parent
where child.id = ?
becomes something like
Code:
SELECT Child.field1, Child.field2, Parent.field1, Parent.field2 ...
FROM Child LEFT JOIN Parent ON
Child.parent_id = Parent.id
WHERE Child.id = ?
while
Code:
from com.Child child
inner join fetch child.parent as parent
where child.id = ?
becomes something like
Code:
SELECT Child.field1, Child.field2, Parent.field1, Parent.field2 ...
FROM Child INNER JOIN Parent ON
Child.parent_id = Parent.id
WHERE Child.id = ?
Since most databases perform an INNER JOIN faster than a LEFT JOIN, you can expect the INNER JOIN to perform slightly better.
Hibernate guys, please check my assumptions and let me know if I'm telling fydoken something wrong.