From b28b462b656bd28874a0be0dba38e4ea4c7f6b3f Mon Sep 17 00:00:00 2001 From: JesseW28 Date: Tue, 2 Mar 2021 13:46:45 +0100 Subject: [PATCH] Add JpaRepository.getById for consistency, deprecated JpaRepository.getOne. Also added two new tests where getById is used instead of getOne. Closes #1697 --- .../data/jpa/repository/JpaRepository.java | 15 +++++++++++++++ .../repository/support/SimpleJpaRepository.java | 13 +++++++++++++ .../AbstractPersistableIntegrationTests.java | 14 ++++++++++++++ .../data/jpa/repository/UserRepositoryTests.java | 10 ++++++++++ 4 files changed, 52 insertions(+) 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 b71daf34bfb..725893aa4ac 100644 --- a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java @@ -31,6 +31,7 @@ * @author Oliver Gierke * @author Christoph Strobl * @author Mark Paluch + * @author Jesse Wouters */ @NoRepositoryBean public interface JpaRepository extends PagingAndSortingRepository, QueryByExampleExecutor { @@ -121,9 +122,23 @@ 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 T getOne(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. + */ + T getById(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 0e03b22385f..b1fd717bfb5 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 @@ -73,6 +73,7 @@ * @author Jens Schauder * @author David Madden * @author Moritz Becker + * @author Jesse Wouters * @param the type of the entity to handle * @param the type of the entity's identifier */ @@ -322,6 +323,7 @@ protected QueryHints getQueryHints() { * (non-Javadoc) * @see org.springframework.data.jpa.repository.JpaRepository#getOne(java.io.Serializable) */ + @Deprecated @Override public T getOne(ID id) { @@ -329,6 +331,17 @@ public T getOne(ID id) { return em.getReference(getDomainClass(), id); } + /* + * (non-Javadoc) + * @see org.springframework.data.jpa.repository.JpaRepository#getById(java.io.Serializable) + */ + @Override + public T getById(ID id) { + + Assert.notNull(id, ID_MUST_NOT_BE_NULL); + return em.getReference(getDomainClass(), id); + } + /* * (non-Javadoc) * @see org.springframework.data.repository.CrudRepository#existsById(java.io.Serializable) diff --git a/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java index 50f4d1ba381..7fa74d1dbb3 100644 --- a/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java @@ -25,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.domain.AbstractPersistable; import org.springframework.data.jpa.domain.sample.CustomAbstractPersistable; +import org.springframework.data.jpa.domain.sample.User; import org.springframework.data.jpa.repository.sample.CustomAbstractPersistableRepository; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -36,6 +37,7 @@ * @author Thomas Darimont * @author Oliver Gierke * @author Jens Schauder + * @author Jesse Wouters */ @Transactional @ExtendWith(SpringExtension.class) @@ -66,4 +68,16 @@ void equalsWorksForProxiedEntities() { assertThat(proxy).isEqualTo(proxy); } + + @Test // gh-1679 + void equalsWorksForProxiedEntitiesUsingGetById() { + + CustomAbstractPersistable entity = repository.saveAndFlush(new CustomAbstractPersistable()); + + em.clear(); + + CustomAbstractPersistable proxy = repository.getById(entity.getId()); + + assertThat(proxy).isEqualTo(proxy); + } } diff --git a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java index a7881df57eb..1d9b8632e1e 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -91,6 +91,7 @@ * @author Kevin Peters * @author Jens Schauder * @author Andrey Kovalev + * @author Jesse Wouters */ @ExtendWith(SpringExtension.class) @ContextConfiguration("classpath:application-context.xml") @@ -987,6 +988,15 @@ void looksUpEntityReference() { assertThat(result).isEqualTo(firstUser); } + @Test // gh-1679 + void looksUpEntityReferenceUsingGetById() { + + flushTestUsers(); + + User result = repository.getById(firstUser.getId()); + assertThat(result).isEqualTo(firstUser); + } + @Test // DATAJPA-415 void invokesQueryWithVarargsParametersCorrectly() {