# SemanticException: inner select

**URL:** <https://discourse.hibernate.org/t/semanticexception-inner-select/11298>\
**Category:** Hibernate ORM\
**Created:** [March 28, 2025, 11:11am UTC](https://discourse.hibernate.org/t/semanticexception-inner-select/11298 "2025-03-28T11:11:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Didem](https://avatars.discourse-cdn.com/v4/letter/d/e5b9ba/32.png) [@Didem](https://discourse.hibernate.org/u/Didem)\
**Post date:** [March 28, 2025, 11:11am UTC](https://discourse.hibernate.org/t/semanticexception-inner-select/11298/1 "2025-03-28T11:11:33Z")

</div>

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:

```auto
    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!

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [March 28, 2025, 2:08pm UTC](https://discourse.hibernate.org/t/semanticexception-inner-select/11298/2 "2025-03-28T14:08:59Z")

</div>

You’re right, this should work just fine. Please try to create a reproducer with our [test case template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) and if you are able to reproduce the issue, create a bug ticket in our [issue tracker](https://hibernate.atlassian.net) and attach that reproducer.

---

<div class="post-metadata">

**Author:** ![Didem](https://avatars.discourse-cdn.com/v4/letter/d/e5b9ba/32.png) [@Didem](https://discourse.hibernate.org/u/Didem)\
**Post date:** [April 4, 2025, 8:02pm UTC](https://discourse.hibernate.org/t/semanticexception-inner-select/11298/3 "2025-04-04T20:02:44Z")

</div>

Thank you for your response.
