Generated SQL when there is more than 1 table in the from clause immediately followed by an inner join results in incorrect SQL and this error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'recipeingr0_.unit_id' in 'on clause'
Example generated SQL:
select
recipeingr0_.itemId as col_0_0_,
recipeingr0_.recipeId as col_1_0_,
item1_.name as col_2_0_,
recipeingr0_.quantity as col_3_0_,
recipeingr0_.unit_id as col_4_0_
from
RecipeIngredient recipeingr0_,
Item item1_
inner join
Unit unit2_
on recipeingr0_.unit_id=unit2_.id
where
recipeingr0_.itemId=item1_.id
and recipeingr0_.recipeId=?
If this is modified to surround both tables in from clause with () then it is parsed correctly.
This is explained in this MySQL bug that was explained as working as designed:
http://bugs.mysql.com/bug.php?id=13551
and this one:
http://bugs.mysql.com/bug.php?id=15952
Described in the 5.0 manual here:
http://dev.mysql.com/doc/refman/5.0/en/join.html
"Previously, the comma operator (,) and JOIN both had the same precedence, so the join expression t1, t2 JOIN t3 was interpreted as ((t1, t2) JOIN t3). Now JOIN has higher precedence, so the expression is interpreted as (t1, (t2 JOIN t3)). This change affects statements that use an ON clause, because that clause can refer only to columns in the operands of the join, and the change in precedence changes interpretation of what those operands are."
The entities where I ran into this:
Item 1-* RecipeIngredient *-1 Recipe
RecipeIngredient *-1 Unit
And the HQL:
select new kh.groceries.domain.RecipeIngredientDetail(ri.itemId, ri.recipeId, ri.item.name, ri.quantity, ri.unit)
from RecipeIngredient ri where ri.recipeId = :recipeId
|