You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECTc.idFROM Document d
LEFT JOINd.contacts c
ON KEY(c) =1
does not render through the restriction part KEY(c) = 1 correctly in the SQL.
The rendered SQL roughly looks like this
SELECTe.idFROM Document d
LEFT JOINd.contacts_join_table c
ONd.id=c.idLEFT JOINc.contacts_entity_table e
ONc.value_id=e.idANDc.contacts_key=1
Assuming one document has 2 contacts, the first join for contacts_join_table will duplicate rows for the document which is wrong.
The condition is wrongly on the second left join which will not cause filtering of duplicates.
The correct SQL for this query and in general a very easy translation looks like this:
SELECTe.idFROM Document d
LEFT JOIN (
d.contacts_join_table c
JOINc.contacts_entity_table e
ONc.value_id=e.id
)
ONd.id=c.idANDc.contacts_key=1
I know this looks odd at first because the parenthesis suggest that aliases within are not available outside, but in case of joins, the parenthesis only give a way of grouping joins. Aliases of the tables are available in the whole query, only the join order is different. This is something that already ANSI SQL-92 specified as joined table in table reference.
So the general idea is to group the join table and target table joins into one join group and apply the ON clause restrictions on that join group.
The text was updated successfully, but these errors were encountered:
andyjefferson
changed the title
Map/List key restrictions in ON clause not rendered appropriately to SQL
Map/List key restrictions in ON clause not rendered appropriately to SQL for join table relations
Mar 15, 2017
A simple JPQL query like
does not render through the restriction part
KEY(c) = 1
correctly in the SQL.The rendered SQL roughly looks like this
Assuming one document has 2 contacts, the first join for contacts_join_table will duplicate rows for the document which is wrong.
The condition is wrongly on the second left join which will not cause filtering of duplicates.
The correct SQL for this query and in general a very easy translation looks like this:
I know this looks odd at first because the parenthesis suggest that aliases within are not available outside, but in case of joins, the parenthesis only give a way of grouping joins. Aliases of the tables are available in the whole query, only the join order is different. This is something that already ANSI SQL-92 specified as joined table in table reference.
So the general idea is to group the join table and target table joins into one join group and apply the ON clause restrictions on that join group.
The text was updated successfully, but these errors were encountered: