Please see this test repo GitHub - gaetannandelec-ibboost/hibernate6-joinsubquery
This works with hibernate 6.5.3-FINAL but fail with any 6.6.X versions.
I have this class example
@Entity
@Table(name = "record_items")
public class RecordItem implements Serializable {
public static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
@Column(name="type_id",insertable = false,updatable = false)
private Long typeId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumnOrFormula(column = @JoinColumn(name = "type_id", referencedColumnName = "entity_id"))
@JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT x.id FROM record_types x WHERE x.entity_id = type_id)", referencedColumnName = "id"))
private RecordType type;
.....
}
@Entity
@Table(name = "record_types")
public class RecordType implements Serializable {
public static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
@Column(name="entity_id")
private Long entityId;
.......
}
The issue lies in the generated SQL for fetch a Record item
With Hibernate 6.6.X :
select ri1_0.id,t1_0.id,t1_0.entity_id,ri1_0.type_id from record_items ri1_0 left join record_types t1_0 on t1_0.entity_id=ri1_0.type_id and t1_0.id=(SELECT x.id FROM record_types xWHERE x.entity_id = ri1_0.type_id) where ri1_0.id=?
it strips the space between the x and the WHERE keyword making the query incorrect