Skip to content
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

Parameter.getType() returns erased generic type for method parameters #2995

Closed
ryerrappa opened this issue Dec 2, 2023 · 3 comments
Closed
Assignees
Labels
type: bug A general bug

Comments

@ryerrappa
Copy link

ryerrappa commented Dec 2, 2023

Hello, I've been facing a fatal exception during application startup upon upgrading to Spring Boot 3.2.

The error message is

Failed to create query for method public abstract long com.example.jparepodemo.dao.RelatedToUserJpaRepository.deleteByIdAndUser_Username(java.lang.Object,java.lang.String); Cannot compare left expression of type 'java.lang.Long' with right expression of type 'java.lang.Object'

It seems to be related to using generics in JPA repositories.

Sample Application

Here is a link to a minimal application that reproduces the issue: https://github.com/ryerrappa/spring-data-jpa-generics-issue

There is also a branch in the above repo called working that shows a working application using a previous version of spring boot. Please note in the branch working, the application will still auto-shutdown, however the application starts up successfully.

This issue occurs when using the following:

  • Spring Boot 3.2
  • Hibernate 6.3, 6.4
  • Java 21
  • H2, Postgres

The issue did not occur when using the following:

  • Spring Boot 2.7, 3.0, 3.1
  • Hibernate default dependency versions
  • Java 17
  • H2, Postgres

Code causing issue

please check https://github.com/ryerrappa/spring-data-jpa-generics-issue for a full sample application

RelatedToUserJpaRepository.java

@NoRepositoryBean
public interface RelatedToUserJpaRepository<T, ID> extends JpaRepository<T, ID> {

    Optional<T> findByIdAndUser_Id(ID entityId, UUID userId);

    Optional<T> findByIdAndUser_Username(ID entityId, String userName);

    long deleteByIdAndUser_Id(ID entityId, UUID userId);

    long deleteByIdAndUser_Username(ID entityId, String username);

    boolean existsByIdAndUser_Id(ID entityId, UUID userId);

    boolean existsByIdAndUser_Username(ID entityId, String username);

    boolean existsByUser_Id(UUID id);

    boolean existsByUser_Username(String username);

}

ManyEntitiesToOneUserJpaRepository.java

@NoRepositoryBean
public interface ManyEntitiesToOneUserJpaRepository<T, ID> extends RelatedToUserJpaRepository<T, ID> {

    Page<T> findByUser_Id(UUID id, Pageable pageable);

    Page<T> findByUser_Username(String username, Pageable pageable);

    long countByUser_Id(UUID id);

    long countByUser_Username(String username);
}

StuffRepository.java

public interface StuffRepository extends ManyEntitiesToOneUserJpaRepository<Stuff, Long> {
}

Error message

Failed to create query for method public abstract long com.example.jparepodemo.dao.RelatedToUserJpaRepository.deleteByIdAndUser_Username(java.lang.Object,java.lang.String); Cannot compare left expression of type 'java.lang.Long' with right expression of type 'java.lang.Object'
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 2, 2023
@mp911de mp911de transferred this issue from spring-projects/spring-data-jpa Dec 4, 2023
@mp911de mp911de changed the title Issues with Spring Data JPA repositories after upgrading to Spring Boot 3.2.0 and Hibernate 6.4. Failed to create query when using generics Parameter.getType() returns erased generic type for method parameters Dec 4, 2023
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 4, 2023
@mp911de
Copy link
Member

mp911de commented Dec 4, 2023

Thanks for reporting the issue. It seems that Hibernate started verifying the parameter type that we have used for quite some while incorrectly. Method parameter types using a generic type have been reduced to their type erasure which is Object in your case.

We need to fix the issue by providing the enclosing type to our method parameters so that we can resolve the correct value type.

@ryerrappa
Copy link
Author

@mp911de thank you!

@jeebendu
Copy link

jeebendu commented May 17, 2024

@ryerrappa @spring-projects-issues
Still i am getting the same issue after upgrading to 3.3.5
Any Suggestion if the case was closed.
Cannot compare left expression of type 'java.lang.Long' with right expression of type 'java.lang.Object'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants