Mixing implicit and explicit JOINs

半城伤御伤魂 提交于 2019-11-27 14:49:02

It results in an error because according to the SQL standard, the JOIN keyword has higher precedence than the comma. The sticky point is that table aliases are not usable until after the corresponding table has been evaluated in the FROM clause.

So when you reference e1 in your JOIN...ON expression, e1 doesn't exist yet.

Please stand by while I research Hibernate and find out if you can persuade it to use JOIN in all cases.


Hmm. Everything at Hibernate.org seems to be redirecting to jboss.org. So no way to read HQL documentation online right now. I'm sure they'll figure out their name serving eventually.

PostgreSQL gives an error too:

ERROR:  invalid reference to FROM-clause entry for table "e1"
LINE 7:     ON e1.managerEmployeeID = e1Manager.employeeID;
               ^
HINT:  There is an entry for table "e1", but it cannot be referenced from this part of the query.

I think the problem is that when you join two tables a and b (e2 and e1Manager in this case) you can only reference those two tables in the "ON" clause. So you can reference e2 and e1Manager in this ON clause, but not e1.

I think this extends so that if you have a chain of "JOIN" statements, you can reference other tables in the same chain in "ON" clauses, but you can't cross a ",". So something like `a JOIN b ON a.a_id = b.a_id JOIN c ON c.b_id = b.b_id AND c.a_id = a.a_id" is allowed.

What's the HQL you are using to produce this SQL? Something like "select e1.name, e2.name, e1.manager.name from Employee e1, Employee e2" ?

Damian Vogel

This might be a bit off topic, because it doesn't concern hibernate at all, but the comment from Bill Karwin really opened my eyes. Instead of writing the implicit joining first, you need to do the explicit joining first. This syntax is especially interesting if you have multiple implicit joins.

Check the following example in MS SQL. Not all contacts have a country code defined, but all contacts have an attribute val which will be looked up in the table Tbl. So the intuitive solution will not work:

SELECT * FROM 
contacts, Tbl
LEFT OUTER JOIN country ON CtryCod = country.CtryCod 
WHERE val = Tbl.val

Instead you might rather want to use the following syntax:

SELECT * FROM 
contacts LEFT OUTER JOIN country ON CtryCod = country.CtryCod, 
Tbl
WHERE val = Tbl.val
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!