Putting the political discussions aside, I've seen the topic of Criteria outer joining several times.

JIRA issues:
------------------------
http://opensource2.atlassian.com/projects/hibernate/browse/HHH-214 (rejected)
http://opensource2.atlassian.com/projects/hibernate/browse/HHH-1137 (open)
http://opensource2.atlassian.com/projects/hibernate/browse/HHH-1005 (mine, rejected)

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=78&t=001047
http://forum.hibernate.org/search.php?search_author=emmanuel.boudrant

and a blog somewhere...

1) Now, the requested feature would be Criteria - not HQL - generation of the following SQL:

SELECT *
FROM foo
LEFT JOIN Bar ON foo.bar_id = bar.id
ORDER BY bar.name;

Instead of:

SELECT *
FROM foo
INNER JOIN Bar ON foo.bar_id = bar.id
ORDER BY bar.name;

2) Where is it needed? On every Criteria query where associations are 0 - N, and using the ORDER BY clause as shown above. This is valid SQL on most databases.

3) Common cases? Any HTML with headers on top that traverses through a simple 0 - N association, such as this:

|    Name     |    Country Name    |    etc....
----------------------------------------------
getName();       getCountry().getName();          etc....

When ordering by country name, the Criteria generates an INNER JOIN. Where ordering by Name, the Criteria isn't generated. The number of results is different when clicking the 'Name' header and clicking the 'Country Name' header. Certainly what the user don't expect.

4) Why Criteria and not HQL? Because when joining for ORDER BY clauses only, the OUTER is much more common than the INNER JOIN. It makes no sense to add a filter only on certain ORDERing ids. And this HTML table example is precisely the kind of things Criterias are used for (usally when creating dynamic filters, which are much harder with HQL).

5) Solution? Add a single overload in Criteria API to createAlias() and createCriteria(). In addition to the current methods, we could put these:

|*createAlias <cid:part1.07030607.02050402@dtqsoftware.com>*(String <http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html> associationPath, String <http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html> alias, JoinMode joinMode); | |*createCriteria <cid:part2.00000901.02080609@dtqsoftware.com>*(String <http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html> associationPath, JoinMode joinMode);

||*createCriteria <cid:part3.08020707.05080303@dtqsoftware.com>*(String <http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html> associationPath, String <http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html> alias, JoinMode joinMode);|

JoinMode would be one of: INNER, LEFT_OUTER, RIGHT_OUTER, I think. It would default to INNER to preserve existing functionality. I guess it would involve changes to Subcriteria class and the appropiate SQL generation code.

It's simple, it's useful, it's needed. Why don't just add the methods?

--
Ing. Leonardo Quijano Vincenzi
Director Técnico
DTQ Software





-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click
_______________________________________________
hibernate-devel mailing list
hibernate-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to