Skip to content

deleteBy returning primitive number queries fail with ClassCastException  #4015

@raitool

Description

@raitool

In 3.5.3 the following worked fully:

@Repository
public interface BookRepository extends JpaRepository<Book, Long> {

    long deleteByTitle(String title);

    Long deleteByAuthor(String author);

    List<Book> deleteByLanguage(String language);
}

In 3.5.4 it does not - deleteByTitle throws one of the following

  • class ee.ooloros.example.springdatajpa.entities.Book cannot be cast to class java.lang.Long
  • Delete query returned more than one element: expected 1, actual 2

I created the following tests to demo what's working (and what's not).

    @Test
    void shouldDeleteOneByTitle() {
        // fails - class ee.ooloros.example.springdatajpa.entities.Book cannot be cast to class java.lang.Long
        long count = bookRepository.deleteByTitle("Pride and Prejudice");
        assertThat(count).isEqualTo(1);
    }

    @Test
    void shouldDeleteMultipleByTitle() {
        // fails - Delete query returned more than one element: expected 1, actual 2
        long count = bookRepository.deleteByTitle("The Trial");
        assertThat(count).isEqualTo(2);
    }

    @Test
    void shouldDeleteOneByAuthor() {
        Long count = bookRepository.deleteByAuthor("Victor Hugo");
        assertThat(count).isEqualTo(1);
    }

    @Test
    void shouldDeleteMultipleByAuthor() {
        Long count = bookRepository.deleteByAuthor("Franz Kafka");
        assertThat(count).isEqualTo(2);
    }

    @Test
    void shouldDeleteByLanguage() {
        List<String> expected = List.of("Don Quixote", "One Hundred Years of Solitude");

        List<Book> deleted = bookRepository.deleteByLanguage("Spanish");
        assertThat(deleted)
                .extracting(Book::getTitle)
                .isEqualTo(expected);
    }

the full code is to recreate/confirm the issue is in https://github.com/raitool/spring-data-jpa-3.5.4-error/blob/main/src/test/java/ee/ooloros/example/springdatajpa/repositories/BookRepositoryTest.java
(and the domain-example was taken from https://www.baeldung.com/spring-data-jpa-delete along with long deleteByTitle(String title);)

PS: The bug was introduced with #3995

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions