From 0926d4bfb634ac626817be9410822e1fa2aa6d1a Mon Sep 17 00:00:00 2001 From: "Greg L. Turnquist" Date: Tue, 4 Jan 2022 09:18:56 -0600 Subject: [PATCH] Introduce JpaRepository.getReferenceById. Introduce a repository method that makes it clear the return value is a reference. Deprecate the previous methods. Closes #2232 Original pull request #2398 --- .../data/jpa/repository/JpaRepository.java | 23 ++++++++++++++++--- .../support/SimpleJpaRepository.java | 20 +++++++++++----- .../AbstractPersistableIntegrationTests.java | 14 ++++++++++- .../jpa/repository/UserRepositoryTests.java | 11 ++++++++- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java index cbc36e210..fb5a437f8 100644 --- a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java @@ -33,6 +33,7 @@ import org.springframework.data.repository.query.QueryByExampleExecutor; * @author Mark Paluch * @author Sander Krabbenborg * @author Jesse Wouters + * @author Greg Turnquist */ @NoRepositoryBean public interface JpaRepository extends PagingAndSortingRepository, QueryByExampleExecutor { @@ -96,7 +97,9 @@ public interface JpaRepository extends PagingAndSortingRepository, * @deprecated Use {@link #deleteAllInBatch(Iterable)} instead. */ @Deprecated - default void deleteInBatch(Iterable entities){deleteAllInBatch(entities);} + default void deleteInBatch(Iterable entities) { + deleteAllInBatch(entities); + } /** * Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs @@ -108,7 +111,6 @@ public interface JpaRepository extends PagingAndSortingRepository, */ void deleteAllInBatch(Iterable entities); - /** * Deletes the entities identified by the given ids using a single query. This kind of operation leaves JPAs first * level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this method. @@ -132,7 +134,7 @@ public interface JpaRepository extends PagingAndSortingRepository, * @param id must not be {@literal null}. * @return a reference to the entity with the given identifier. * @see EntityManager#getReference(Class, Object) for details on when an exception is thrown. - * @deprecated use {@link JpaRepository#getById(ID)} instead. + * @deprecated use {@link JpaRepository#getReferenceById(ID)} instead. */ @Deprecated T getOne(ID id); @@ -146,10 +148,25 @@ public interface JpaRepository extends PagingAndSortingRepository, * @param id must not be {@literal null}. * @return a reference to the entity with the given identifier. * @see EntityManager#getReference(Class, Object) for details on when an exception is thrown. + * @deprecated use {@link JpaRepository#getReferenceById(ID)} instead. * @since 2.5 */ + @Deprecated T getById(ID id); + /** + * Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is + * implemented this is very likely to always return an instance and throw an + * {@link javax.persistence.EntityNotFoundException} on first access. Some of them will reject invalid identifiers + * immediately. + * + * @param id must not be {@literal null}. + * @return a reference to the entity with the given identifier. + * @see EntityManager#getReference(Class, Object) for details on when an exception is thrown. + * @since 2.7 + */ + T getReferenceById(ID id); + /* * (non-Javadoc) * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example) diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 5f8a82715..0fbc14f20 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -331,26 +331,34 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation