[JIRA] (HHH-16888) Left join with exists subquery adds extra join
by Dan Mercean (JIRA)
Dan Mercean ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=712020%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYzNjYmI4YTk5... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16888?atlOrigin=eyJpIjoiYzNjYm... ) HHH-16888 ( https://hibernate.atlassian.net/browse/HHH-16888?atlOrigin=eyJpIjoiYzNjYm... ) Left join with exists subquery adds extra join ( https://hibernate.atlassian.net/browse/HHH-16888?atlOrigin=eyJpIjoiYzNjYm... )
Change By: Dan Mercean ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=712020%... )
Having two unrelated entities:
{noformat}@Entity
public class Entity1 {
@Id
@GeneratedValue
private Long id;
private String uuid;
...
}{noformat}
{noformat}@Entity
public class Entity2 {
@Id
@GeneratedValue
private Long id;
private String uuid;
@ManyToMany(fetch = FetchType.LAZY)
private List<Property> properties;
...
}{noformat}
And the {{Property}}:
{noformat}@Entity
public class Property {
@Id
@GeneratedValue
private Long id;
private String name;
...
}{noformat}
We are trying to get both Entities, but left join only the Entities that have a specific Property:
{noformat} List<Object[]> results = entityManager.createQuery(
"select e1, e2 " +
"from Entity1 e1 " +
"left join Entity2 e2 on e1.uuid = e2.uuid and exists (" +
"select 1 from e2.properties p where p.name = :propertyName)")
.setParameter("propertyName", "admin")
.getResultList();{noformat}
So we are interested only in the Entity2 that have a Property named {{admin}}. This is just an example, our actual use case is more complicated.
This was working fine before hibernate Hibernate 6, generating the following query:
{noformat}select
entity1x0_.id as id1_0_0_,
entity2x1_.id as id1_1_1_,
entity1x0_.uuid as uuid2_0_0_,
entity2x1_.uuid as uuid2_1_1_
from
Entity1 entity1x0_
left outer join
Entity2 entity2x1_
on (
entity1x0_.uuid=entity2x1_.uuid
and (
exists (
select
1
from
Entity2_Property properties2_,
Property property3_
where
entity2x1_.id=properties2_.Entity2_id
and properties2_.properties_id=property3_.id
and property3_.name=?
)
)
){noformat}
But with Hibernate 6, another join is added at the end which duplicates the results and breaks the left join:
{noformat}select
e1_0.id,
e1_0.uuid,
e2_0.id,
e2_0.uuid
from
Entity1 e1_0
left join
Entity2 e2_0
on e1_0.uuid=e2_0.uuid
and exists(select
1
from
Entity2_Property p1_0
join
Property p1_1
on p1_1.id=p1_0.properties_id
where
p1_1.name=?
and e2_0.id=p1_0.Entity2_id)
join
Entity2_Property p2_0
on e2_0.id=p2_0.Entity2_id{noformat}
Join that has no purpose:
{noformat}join
Entity2_Property p2_0
on e2_0.id=p2_0.Entity2_id{noformat}
Created test cases for both Hibernate 5 and 6: [https://github.com/danmercean/hibernate-test-case-left-join-exists|https:...]
( https://hibernate.atlassian.net/browse/HHH-16888#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16888#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#100228- sha1:4a42edc )
2 years, 6 months
[JIRA] (HHH-16888) Left join with exists subquery adds extra join
by Dan Mercean (JIRA)
Dan Mercean ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=712020%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMGNkOWU1YTBj... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16888?atlOrigin=eyJpIjoiMGNkOW... ) HHH-16888 ( https://hibernate.atlassian.net/browse/HHH-16888?atlOrigin=eyJpIjoiMGNkOW... ) Left join with exists subquery adds extra join ( https://hibernate.atlassian.net/browse/HHH-16888?atlOrigin=eyJpIjoiMGNkOW... )
Issue Type: Bug Affects Versions: 6.1.7, 6.2.6 Assignee: Unassigned Components: hibernate-core Created: 04/Jul/2023 05:54 AM Priority: Major Reporter: Dan Mercean ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=712020%... )
Having two unrelated entities:
@Entity
public class Entity1 {
@Id
@GeneratedValue
private Long id;
private String uuid;
...
}
@Entity
public class Entity2 {
@Id
@GeneratedValue
private Long id;
private String uuid;
@ManyToMany(fetch = FetchType.LAZY)
private List<Property> properties;
...
}
And the Property :
@Entity
public class Property {
@Id
@GeneratedValue
private Long id;
private String name;
...
}
We are trying to get both Entities, but left join only the Entities that have a specific Property:
List<Object[]> results = entityManager.createQuery(
"select e1, e2 " +
"from Entity1 e1 " +
"left join Entity2 e2 on e1.uuid = e2.uuid and exists (" +
"select 1 from e2.properties p where p.name = :propertyName)")
.setParameter("propertyName", "admin")
.getResultList();
So we are interested only in the Entity2 that have a Property named admin. This is just an example, our actual use case is more complicated.
This was working fine before hibernate 6, generating the following query:
select
entity1x0_.id as id1_0_0_,
entity2x1_.id as id1_1_1_,
entity1x0_.uuid as uuid2_0_0_,
entity2x1_.uuid as uuid2_1_1_
from
Entity1 entity1x0_
left outer join
Entity2 entity2x1_
on (
entity1x0_.uuid=entity2x1_.uuid
and (
exists (
select
1
from
Entity2_Property properties2_,
Property property3_
where
entity2x1_.id=properties2_.Entity2_id
and properties2_.properties_id=property3_.id
and property3_.name=?
)
)
)
But with Hibernate 6, another join is added at the end which duplicates the results and breaks the left join:
select
e1_0.id,
e1_0.uuid,
e2_0.id,
e2_0.uuid
from
Entity1 e1_0
left join
Entity2 e2_0
on e1_0.uuid=e2_0.uuid
and exists(select
1
from
Entity2_Property p1_0
join
Property p1_1
on p1_1.id=p1_0.properties_id
where
p1_1.name=?
and e2_0.id=p1_0.Entity2_id)
join
Entity2_Property p2_0
on e2_0.id=p2_0.Entity2_id
Join that has no purpose:
join
Entity2_Property p2_0
on e2_0.id=p2_0.Entity2_id
Created test cases for both Hibernate 5 and 6: https://github.com/danmercean/hibernate-test-case-left-join-exists
( https://hibernate.atlassian.net/browse/HHH-16888#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16888#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#100228- sha1:4a42edc )
2 years, 6 months
[JIRA] (HHH-16886) Incorrect SQL alternative for tuple-in-lists on DBMS not supporting tuple-in-lists
by Dennis Katz (JIRA)
Dennis Katz ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5f6b7a7... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNjNlMmU2NmQ3... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16886?atlOrigin=eyJpIjoiNjNlMm... ) HHH-16886 ( https://hibernate.atlassian.net/browse/HHH-16886?atlOrigin=eyJpIjoiNjNlMm... ) Incorrect SQL alternative for tuple-in-lists on DBMS not supporting tuple-in-lists ( https://hibernate.atlassian.net/browse/HHH-16886?atlOrigin=eyJpIjoiNjNlMm... )
Change By: Dennis Katz ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5f6b7a7... )
Hibernate generates in for some DBMS wrong SQL in special cases. In my case, i have an Spring application that querys an table with an collection of an embeddable key with two fields and one additional boolean value (similar as in-List with tuples - which is not supported by DB2).
In this case, the generated SQL returns all rows that matches the embeddable expect for the last - for this the row must match the embeddable and the boolean value.
h3. Example
*Table-Definition*
{noformat}No Name Typ
1 id int(11)
2 key_1 char(3)
3 key_2 char(3)
4 present char(1)
{noformat}
*Data*
{noformat} id key_1 key_2 present
1 ZH2 HDS Y
2 ZH3 HDS Y
3 GXZ HDS N
4 KAZ TST Y{noformat}
*Query-Method (Spring Data JPA)*
{{public List<TestEntity> findAllBySubInAndPresent(Collection<Sub> subs, String present);}}
*Model*
{noformat}@AllArgsConstructor
@Data
@Entity
@Table(name = "tester")
public class TestEntity {
@Id
@Column(name = "id")
private int id;
@Embedded
private Sub sub;
@Column(name = "present")
private String present;
@RequiredArgsConstructor
@AllArgsConstructor
@Data
@Embeddable
public static class Sub {
@Column(name = "key_1")
private String key1;
@Column(name = "key_2")
private String key2;
}
}{noformat}
*generated wrong SQL:* {{select t1_0.id,t1_0.present,t1_0.key_1,t1_0.key_2 from tester t1_0 where (t1_0.key_1=? and t1_0.key_2=?) or (t1_0.key_1=? and t1_0.key_2=?) or (t1_0.key_1=? and t1_0.key_2=?) and t1_0.present=? }}
Which returns the rows with the IDs: 1, 3 and 4.
Expected are only the rows with the IDs: 1 and 4
*This is fixed by adding parenthesis around the ORs of the Collection-Data:* {{select t1_0.id,t1_0.present,t1_0.key_1,t1_0.key_2 from tester t1_0 where ((t1_0.key_1=? and t1_0.key_2=?) or (t1_0.key_1=? and t1_0.key_2=?) or (t1_0.key_1=? and t1_0.key_2=?)) and t1_0.present=?}}
This incorrect SQL generation only occours for DBMS that do not support the {{supportsRowValueConstructorSyntaxInInList}}. For example DB2.
This problem occurs in versions 5.x and 6.x.
_I've allready fixed this issue and i'll submit an PR the next hours._
( https://hibernate.atlassian.net/browse/HHH-16886#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16886#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#100228- sha1:4a42edc )
2 years, 6 months