[JIRA] (HHH-16956) JPQL to SQL translation : wrong column selection using “select new” and “right join”
by Olivier Jaulent (JIRA)
Olivier Jaulent ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=61bcdf8... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNWM1ZjM4M2M5... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16956?atlOrigin=eyJpIjoiNWM1Zj... ) HHH-16956 ( https://hibernate.atlassian.net/browse/HHH-16956?atlOrigin=eyJpIjoiNWM1Zj... ) JPQL to SQL translation : wrong column selection using “select new” and “right join” ( https://hibernate.atlassian.net/browse/HHH-16956?atlOrigin=eyJpIjoiNWM1Zj... )
Issue Type: Bug Affects Versions: 6.2.5 Assignee: Unassigned Created: 20/Jul/2023 01:20 AM Environment: Java 17 / Springboot 3.1 Priority: Major Reporter: Olivier Jaulent ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=61bcdf8... )
Hi,
When migrating from springboot 2.7 with (hibernate 5.6.14.Final) to springboot 3.1 (hibernate 6.2.6.Final) my generated sql queries from jpql are wrongs.
Inpacted queries are those with "select new" and "right join" : I use this to create a projection called MyProjection from two entities E & C.
I need projection even if entity C is non existant.
I need id of the E entity every time in my projection.
With hibernate 5.6.14.Final it works (it works for a long time and with anterior hibernate versions).
But withhibernate 6.2.6.Final I encounter a problem : the select always use the entity C forein key to E instead of the id of the E entity.
So, when C is null, the id of the E entity is null in my projection.
Is this a bug ? Or something else ?
Exemple :
*Entity C*
@Entity
public class EntityC implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String reference;
@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
@JoinColumn(name = "entity_e_id")
private EntityE entityE;
}
*Entity E*
@Entity
public class EntityE implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
*My Projection*
public class MyProjection implements Serializable {
private static final long serialVersionUID = -3431863476946276142L;
private Long entityEId;
private Long entityCId;
private String entityEName;
private String entityCReference;
public MyProjection(
Long entityEId,
Long entityCId,
String entityEName,
String entityCReference) {
this.entityEId = entityEId;
this.entityCId = entityCId;
this.entityEName = entityEName;
this.entityCReference = entityCReference;
}
public MyProjection(
Long entityEId,
Long entityCId) {
this.entityEId = entityCId;
this.entityEId = entityCId;
}
public MyProjection() {}
}
*Queries*
JPQL Query
select new MyProjection(e.id, c.id, e.name, c.reference)
from EntityC c right join c.entityE e;
Hibernate 5.6.14.Final Generated SQL
select e1_.id as col_0_0_, c0_.id as col_1_0_, e1_.name as col_2_0_, c0_.reference as col_3_0_
from entity_c c0_
right outer join entity_e e1_ on c0_.entity_e_id=e1_.id;
Hibernate 6.2.6.Final Generated SQL
select c0_.entity_e_id as col_0_0_, c0_.id as col_1_0_, e1_.name as col_2_0_, c0_.reference as col_3_0_
from entity_c c0_
right join entity_e e1_ on c0_.entity_e_id=e1_.id;
( https://hibernate.atlassian.net/browse/HHH-16956#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16956#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#100232- sha1:304e1c7 )