Validation failed for query for method upgrading from Spring Boot 3.1.5 to 3.2.0
Asked Answered
M

1

4

I upgraded from Spring Boot (Data) 3.1.5 to 3.2.0.

Several of my queries in the Repositories throw exceptions now on application start in the validation phase:

Caused by: Cannot compare left expression of type 'java.lang.Object' with right expression of type 'java.lang.Long'.

E.g.:

        @Query(value = "SELECT new com.my.service.crud.ReferencedObject(p.id, p.name, r.name) FROM MyEntityA p join MyEntityB r on (p.aId = r.id) WHERE " +
                       " p.aId = :projectId and exists (select id from EntityC where cId = p.id and dTypeId = :uutId)")
        List<ReferencedObject> findReferencesToUtilType(@Param("projectId") Long projectId, @Param("uutId") Long utilTypeId);

This (anonymized) Code worked fine with 3.1.5 and returned a list of ReferencedObjects. I even get errors on finders that return Optional<Entity> and e.g. findByIdAndOtherAttr(...).

The underlying cause is

Caused by: org.hibernate.query.SemanticException: Cannot compare left expression of type 'java.lang.Long' with right expression of type 'java.lang.Object'

at org.hibernate.query.sqm.internal.TypecheckUtil.assertComparable(TypecheckUtil.java:338)
at org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate.<init>(SqmComparisonPredicate.java:48)
at org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate.<init>(SqmComparisonPredicate.java:34)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.createComparisonPredicate(SemanticQueryBuilder.java:2447)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitComparisonPredicate(SemanticQueryBuilder.java:2391)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitComparisonPredicate(SemanticQueryBuilder.java:268)
at org.hibernate.grammars.hql.HqlParser$ComparisonPredicateContext.accept(HqlParser.java:6071)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitGroupedPredicate(SemanticQueryBuilder.java:2251)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitGroupedPredicate(SemanticQueryBuilder.java:268)
at org.hibernate.grammars.hql.HqlParser$GroupedPredicateContext.accept(HqlParser.java:5994)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.consumeJoin(SemanticQueryBuilder.java:2129)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitEntityWithJoins(SemanticQueryBuilder.java:1921)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitFromClause(SemanticQueryBuilder.java:1900)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuery(SemanticQueryBuilder.java:1147)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuerySpecExpression(SemanticQueryBuilder.java:940)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuerySpecExpression(SemanticQueryBuilder.java:268)
at org.hibernate.grammars.hql.HqlParser$QuerySpecExpressionContext.accept(HqlParser.java:1844)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimpleQueryGroup(SemanticQueryBuilder.java:925)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimpleQueryGroup(SemanticQueryBuilder.java:268)
at org.hibernate.grammars.hql.HqlParser$SimpleQueryGroupContext.accept(HqlParser.java:1718)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSelectStatement(SemanticQueryBuilder.java:442)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitStatement(SemanticQueryBuilder.java:401)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.buildSemanticModel(SemanticQueryBuilder.java:310)
at org.hibernate.query.hql.internal.StandardHqlTranslator.translate(StandardHqlTranslator.java:71)
at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.createHqlInterpretation(QueryInterpretationCacheStandardImpl.java:165)
at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.resolveHqlInterpretation(QueryInterpretationCacheStandardImpl.java:147)
at org.hibernate.internal.AbstractSharedSessionContract.interpretHql(AbstractSharedSessionContract.java:744)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:794)
... 43 common frames omitted

I found this https://discourse.hibernate.org/t/upgrade-to-6-3-rc1-cannot-compare-left-expression-of-type-java-sql-timestamp-with-right-expression-of-type-com-model-entity-impl-user/8034/5 where the Hibernate Team member said, that one is comparing apples with oranges...

Does anyone have a link to a migration guide?

What do I have to do to get my queries working again?

In the release notes of Spring Boot 3.2.0 I only found a hint that "deprecated features have been dereleased now".

Thanks for any hint!

Mccraw answered 24/11, 2023 at 6:58 Comment(1)
It looks like this is a bug in spring-boot 3.2 => github.com/spring-projects/spring-data-jpa/issues/3239 and will be fixed soon. The bug is already closed. Hibernate 6.3 had the bug and spring did not support 6.4 where it was fixed. So the delivery of the above issue will hopefully fix my problem.Mccraw
M
1

We found the problem. We are using an abstract super class for all Entities. In this class we use generics for the ID class (with Entity being an interface with T getId(); method:

public abstract class AbstractEntity<T> implements Entity<T> {
@Id
private T id;

This worked until Spring Boot Data 3.1.5.

From 3.2.0 on the T class must not be a generic type. In our case it must extend Long in order to work, since we use a Long for all ids... :

public abstract class AbstractEntity<T extends Long> implements Entity<T> {
@Id
private T id;

The generic was introduced with composite Id-classes in mind.

We will change the code and use Long directly, since we never use composite ids in this project.

That was all. Not a spring boot / spring data jpa bug or the like.

Hope this helps somebody else.

Mccraw answered 27/11, 2023 at 12:16 Comment(2)
Is this a workaround or a solution? Thanks.Stupefy
It is not a workaround, it's a solution. Problem was with generic T since V3.2.0. Check your code for incompatible types or generics, if you have a similar problemMccraw

© 2022 - 2024 — McMap. All rights reserved.