Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
leshalv opened this issue Nov 21, 2023 · 9 comments
Labels
for: external-project For an external project and not something we can fix

Comments

@leshalv
Copy link

leshalv commented Nov 21, 2023

Caused by: org.hibernate.query.SemanticException: Cannot compare left expression of type 'java.io.Serializable' with right expression of type 'java.lang.Long'
	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.sqm.internal.SqmCriteriaNodeBuilder.equal(SqmCriteriaNodeBuilder.java:2070)
	at org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.equal(SqmCriteriaNodeBuilder.java:189)
	at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:312)
	at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:210)
	at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:121)
	at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:60)
	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:119)
	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:95)
	at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:81)
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:218)
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:361)
	at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:102)
	... 54 common frames omitted

Use this example to reproduce:

issues-3235.zip

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 21, 2023
@mp911de
Copy link
Member

mp911de commented Nov 21, 2023

This is a Hibernate issue caused by your configuration setup. Hibernate sees only the Serializable part on the query and fails instead of properly applying long comparison.

This issue can be easily reproduced with:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<UserEntity> query = cb.createQuery(UserEntity.class);
Root<UserEntity> from = query.from(UserEntity.class);
query.where(cb.equal(from.get("id"), cb.parameter(Long.class)));

Please file a bug report at Hibernate.

Note that passing on the actual value works:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<UserEntity> query = cb.createQuery(UserEntity.class);
Root<UserEntity> from = query.from(UserEntity.class);
query.where(cb.equal(from.get("id"), 1L));

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Nov 21, 2023
@mp911de mp911de added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 21, 2023
@leshalv
Copy link
Author

leshalv commented Nov 22, 2023

@drewhk
Copy link

drewhk commented Nov 30, 2023

We get this same issue for all repository interfaces where there are generics involved. E.g.

findByIdAndActiveTrue(java.lang.Object); Cannot compare left expression of type 'java.lang.Long' with right expression of type 'java.lang.Object'

where Object comes into play only because of type erasure. We are just using plain CRUDPagingAndSortingRepository<T, ID>, JpaRepository<T, ID> and the @Query annotation, no criteria API is used at all.

I hope Hibernate fixes this soon.

@Herzenkin
Copy link

Herzenkin commented Feb 15, 2024

Hi @mp911de !

I'm in the middle of upgrading to SpringBoot 3.2.2 and I'm still suffering from the very similar issue (just quite simple HQL query with JOIN statement):

org.hibernate.query.SemanticException: Cannot compare left expression of type 'java.lang.Long' with right expression of type com.custom.BaseEntity

I see that I'm not alone with this problem - as there is an open ticket to Hibernate ORM (https://hibernate.atlassian.net/browse/HHH-17595). And seems like there is some integration issue with Spring Data as this issue cannot be reproduced just with bare Hibernate 6.4.1.

@amirensit
Copy link
Contributor

I am facing the same error.
Any solution ?

@maitrisheth65
Copy link

I am trying to update spring from 2.7.3 to 3.2.3 and I am facing the same issue

@amirensit
Copy link
Contributor

amirensit commented Feb 28, 2024

For my case I went back to hibernate 6.2 versions.

@wagnercosta-elo
Copy link

Alguma novidade sobre ?

@rishiraj88
Copy link

Alguma novidade sobre ?
Não, nenhum ainda. No news yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

9 participants