Hibernate 6.6.X regression with join formula

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

Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.

Problem is caused by StringTokenizer skipping some tokens when reaching some token contained in list of literal prefixes that is just ordinary token. In this case that was x. To solve this temporary lookahead StringTokenizer should be used, like it is used in main branch.

I’ve changed code mentioned in link above to fit Hibernate Core tests and proposed a solution to that problem - Jira issue HHH-19052

1 Like