SemanticException: inner select

Hi,

I’m using Hibernate-core 6.6.2.Final with Spring Boot 3.3.9.
In one of my projects, I encountered the following error:

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:406)*
  • …*
    Wrapped by: java.lang.IllegalArgumentException: org.hibernate.query.SemanticException: Cannot compare left expression of type ‘java.lang.Long’ with right expression of type ‘java.lang.Object’
  • at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:143)*
  • …*
    Wrapped by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract my.pkg.Entity my.pkg.MyRepository.findMinEntity(java.util.List, java.lang.Long, my.pkg.Status)
  • at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:100)*
  • …*
    Wrapped by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract my.pkg.Entity my.pkg.MyRepository.findMinEntity(…); Reason: Validation failed…

The related query is:

    SELECT e FROM MyEntity e 
    WHERE e.id = (
        SELECT MIN(sub.id) 
        FROM MyEntity sub 
        WHERE sub.id >= :startId 
        AND sub.status = :status 
        AND sub.source.groupId NOT IN (:excludedGroupIds) 
        AND sub.source.key IS NOT NULL
    )")
MyEntity findMinEntity(
    @Param("excludedGroupIds") List<Long> excludedGroupIds, 
    @Param("startId") Long startId, 
    @Param("status") Status status
);

As a workaround, I rewrote the query without the subquery and it worked correctly.
Is this a known issue or expected behavior in Hibernate 6?
Shouldn’t comparing Long with MIN(id) work as expected?

Thanks in advance!

You’re right, this should work just fine. 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.

Thank you for your response.