Error with @Query in hibernate 6.2.2

This was working fine with hibernate 5 but breaking in hibernate 6.2

Query used : @Query(“select t1 from tableA t1 where t1.code = :code and t1.columnA not in (2,3,6,7)”)

We are getting error :
Caused by: java.lang.IllegalArgumentException: Can’t compare test expression of type [columnA ] with element of type [basicType@5(java.lang.Integer,4)]
at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.assertComparable(SqmCriteriaNodeBuilder.java:2098) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.sqm.tree.predicate.SqmInListPredicate.implyListElementType(SqmInListPredicate.java:138) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.sqm.tree.predicate.SqmInListPredicate.(SqmInListPredicate.java:60) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitInPredicate(SemanticQueryBuilder.java:2669) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitInPredicate(SemanticQueryBuilder.java:253) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.grammars.hql.HqlParser$InPredicateContext.accept(HqlParser.java:6086) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitAndPredicate(SemanticQueryBuilder.java:2314) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitAndPredicate(SemanticQueryBuilder.java:253) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.grammars.hql.HqlParser$AndPredicateContext.accept(HqlParser.java:6013) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitWhereClause(SemanticQueryBuilder.java:2297) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitWhereClause(SemanticQueryBuilder.java:253) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.grammars.hql.HqlParser$WhereClauseContext.accept(HqlParser.java:5910) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuery(SemanticQueryBuilder.java:1161) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuerySpecExpression(SemanticQueryBuilder.java:937) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuerySpecExpression(SemanticQueryBuilder.java:253) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.grammars.hql.HqlParser$QuerySpecExpressionContext.accept(HqlParser.java:1818) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimpleQueryGroup(SemanticQueryBuilder.java:931) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimpleQueryGroup(SemanticQueryBuilder.java:253) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.grammars.hql.HqlParser$SimpleQueryGroupContext.accept(HqlParser.java:1711) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSelectStatement(SemanticQueryBuilder.java:418) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitStatement(SemanticQueryBuilder.java:377) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.buildSemanticModel(SemanticQueryBuilder.java:295) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
at org.hibernate.query.hql.internal.StandardHqlTranslator.translate(StandardHqlTranslator.java:81) ~[hibernate-core-6.2.2.Final.jar:6.2.2.Final]
… 90 common frames omitted

Hibernate 6.2 introduced type compatibility checks when comparing expressions. This is what is failing in your case, but without seeing the entity mappings and / or the the query you’re trying to execute I can’t help you further.

If you think this is a bug, please create an issue in the issue tracker (https://hibernate.atlassian.net) with a test case (GitHub - hibernate/hibernate-test-case-templates: Templates and examples to report issues to Hibernate) that reproduces the issue.

PFB entity mappings:

@Data
@Entity
@Table(name = "tableA ")
public class tableA {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "pk")
private long objectId;

@ManyToOne
@JoinColumn(name = "columnA", nullable = false)
private TableB columnA;

}

@Data
@Entity
@Table(name = “TableB”)
public class tableB {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "pk1")
private long objectId;

@OneToMany(mappedBy = "columnA",
		cascade = CascadeType.ALL,
		orphanRemoval = true)
@JsonIgnore
private List<String> listObj = new ArrayList<>();

}

You’re trying to compare columnA is of entity type TableB to an integer value. This used to work in Hibernate 5, as it defaulted to the entity’s primary key, but now it’s explicitly forbidden.

You can simply use t1.columnA.objectId not in (2,3,6,7) instead and your query should work correctly again.

1 Like

@mbladel Thanks a lot. It worked. Spent a lot of time analyzing this issue , do we have any clear documents for such issues.

You can find the information about this specific case in this paragraph of the 6.0 migration guide.

See also the 6.2 migration guide, which contains links to previous versions.

1 Like

Does any help i am also having same issue

yes . share your query

SELECT l " +
“FROM LocationEntity l " +
" JOIN (” +
" SELECT ls.locationId AS locationId , COUNT(ls.locationId) AS freq " +
" FROM LogisticsScheduleEntity ls " +
" JOIN ls.logisticsVoyage lv " +
" WHERE (lv.userUpdatedBy = :userId " +
" OR lv.userNominationBy = :userId " +
" OR lv.userFixedBy = :userId )" +
" GROUP BY ls.locationId" +
") ids ON ids.locationId = l.locationId " +
"ORDER BY ids.freq DESC

Could you share your error here and share the both entity details