[JIRA] (HHH-16487) Invalid SQL query with unexpected join created for JPQL where clause
by Dennis van Holsteijn (JIRA)
Dennis van Holsteijn ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMjUxZjE3Njk1... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16487?atlOrigin=eyJpIjoiMjUxZj... ) HHH-16487 ( https://hibernate.atlassian.net/browse/HHH-16487?atlOrigin=eyJpIjoiMjUxZj... ) Invalid SQL query with unexpected join created for JPQL where clause ( https://hibernate.atlassian.net/browse/HHH-16487?atlOrigin=eyJpIjoiMjUxZj... )
Issue Type: Bug Affects Versions: 6.1.7, 6.2.0, 6.2.1 Assignee: Unassigned Attachments: class-hierarchy.png Components: hibernate-core Created: 18/Apr/2023 06:47 AM Priority: Major Reporter: Dennis van Holsteijn ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
JPQL where clause causes unexpected joins in resulting SQL query in the following scenario:
* Class hierarchy with @Inheritance(strategy = InheritanceType.JOINED) , see image:
* Class ImageItem has self-reference via ImageItem#original and ImageItem#thumbnail
The following JPQL query:
select i from ImageItem i where i.original is null and i.thumbnail is not null
creates SQL query:
Hibernate:
select
i1_0.id,
case
when i1_1.id is not null then 3
when i1_0.id is not null then 1
when i1_2.id is not null then 0
end,
i1_2.fileName,
i1_0.height,
i1_0.original_id,
i1_0.thumbnail_id,
i1_0.width,
i1_1.croppedHeight,
i1_1.croppedWidth,
i1_1.leftPosition,
i1_1.presetKey,
i1_1.topPosition
from
ImageItem i1_0
left join
PartialImage i1_1
on i1_0.id=i1_1.id
join
MediaItem i1_2
on i1_0.id=i1_2.id
join
ImageItem o1_0
on o1_0.id=i1_0.original_id
join
ImageItem t1_0
on t1_0.id=i1_0.thumbnail_id
where
i1_0.original_id is null
and i1_0.thumbnail_id is not null
Due to the inner join:
join ImageItem o1_0 on o1_0.id=i1_0.original_id
This query will never return ImageItems where original is null!
See testcase: https://github.com/dvholsteijn/hibernate-6-invalid-sql/tree/main
In hibernate 5.6.15 a correct query is created, see testcase: https://github.com/dvholsteijn/hibernate-6-invalid-sql/tree/hibernate-5
( https://hibernate.atlassian.net/browse/HHH-16487#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16487#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100222- sha1:c08beee )