Skip to content

Unexpected EntityNotFoundException thrown from JpaRepository #3958

@oliver-b-theissen

Description

@oliver-b-theissen

Hi Spring Team,

Environment

  1. Spring Boot version 3.5.4
  2. Using spring-boot-starter-data-jpa
  3. H2 in memory data base

Steps to reproduce

  1. Declare a JpaRepository for any entity like public interface AccountRepository implements JpaRepository<Account,Long>
  2. Declare AccountRepository accountRepository and let Springframework inject the repository.
  3. Call accountRepository.getReferenceById(id) with an non-null id value which is not yet persisted.

Expected

As per Springs @Repository abstraction, a IncorrectResultSizeDataAccessException is thrown.

Actual

JPAs EntityNotFoundException is thrown.

Comments

This breaks the abstraction of Springs @Repository over the underlying persistence layer / data store.

Other methods in JpaRepository convert from JPA specific exceptions into Springs DataAccessException hierarchy. Example: the method save(null) throws InvalidDataAccessApiUsageException. Remark: the Javadoc for save(id) is wrong: it says IllegalArgumentException.

Best,
Oliver

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions