Skip to content

Issues with Spring Data JPA repositores after upgrading to Spring Boot 3.2.0 and Hibernate 6.4. Failed to create query when using generics #38635

Closed as not planned
@ryerrappa

Description

@ryerrappa

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectFor an external project and not something we can fixstatus: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions