Note that there are some explanatory texts on larger screens.

plurals
  1. POMixing implicit and explicit JOINs
    primarykey
    data
    text
    <p>I am having a problem with Hibernate generating invalid SQL. Specifically, mixing and matching implicit and explicit joins. This seems to be an <a href="http://opensource.atlassian.com/projects/hibernate/browse/HHH-3388" rel="noreferrer">open bug</a>.</p> <p>However, I'm not sure <em>why</em> this is invalid SQL. I have come up with a small toy example that generates the same syntax exception.</p> <h2>Schema</h2> <pre><code>CREATE TABLE Employee ( employeeID INT, name VARCHAR(255), managerEmployeeID INT ) </code></pre> <h2>Data</h2> <pre><code>INSERT INTO Employee (employeeID, name) VALUES (1, 'Gary') INSERT INTO Employee (employeeID, name, managerEmployeeID) VALUES (2, 'Bob', 1) </code></pre> <h2>Working SQL</h2> <p>Both of these queries work. I realize there is a Cartesian product; that's intentional.</p> <p><em>Explicit JOIN:</em></p> <pre><code>SELECT e1.name, e2.name, e1Manager.name FROM Employee e1 CROSS JOIN Employee e2 INNER JOIN Employee e1Manager ON e1.managerEmployeeID = e1Manager.employeeID </code></pre> <p><em>Implicit JOIN:</em></p> <pre><code>SELECT e1.name, e2.name, e1Manager.name FROM Employee e1, Employee e2, Employee e1Manager WHERE e1.managerEmployeeID = e1Manager.employeeID </code></pre> <h2>Invalid SQL</h2> <p>This query does NOT work on MSSQL 2000/2008 or MySQL:</p> <pre><code>SELECT e1.name, e2.name, e1Manager.name FROM Employee e1, Employee e2 INNER JOIN Employee e1Manager ON e1.managerEmployeeID = e1Manager.employeeID </code></pre> <p>In MS2000, I get the error: </p> <blockquote> <p>The column prefix 'e1' does not match with a table name or alias name used in the query.</p> </blockquote> <p>In MySQL, the error is:</p> <blockquote> <p>Unknown column 'e1.managerEmployeeID' in 'on clause'.</p> </blockquote> <h2>Question(s)</h2> <ol> <li>Why is this syntax invalid?</li> <li><strong>Bonus:</strong> Is there a way to force Hibernate to use only explicit JOINs?</li> </ol> <hr>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload