Skip to content

Switch to JPA 3.2 Query.getSingleResultOrNull() #3701

Closed
@david-kubecka

Description

@david-kubecka

I'm talking about this code: https://github.com/spring-projects/spring-data-jpa/blob/main/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java#L224. As we know, getSingleResult throws an exception upon a non-existing entity. Since this is an allowed situation the exception is caught here https://github.com/spring-projects/spring-data-jpa/blob/main/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java#L92.

I wonder why this try/catch approach (generally frowned upon) is used here instead of an alternative approach using getResultList, see e.g. https://stackoverflow.com/a/2885024/13015027. Apart from a slight performance penalty (not measured), the biggest problem of the current approach is that the exception is captured by telemetry agents (see open-telemetry/opentelemetry-java-instrumentation#12125).

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions