From 3fb5e7d186a2a7cb61248050ffdc67316d3724e9 Mon Sep 17 00:00:00 2001 From: Jesse Wouters Date: Wed, 3 Mar 2021 11:44:41 +0100 Subject: [PATCH] DATAJPA-1377 added JpaRepository.getById, deprecated JpaRepository.getOne Closes #1697 --- .../data/jpa/repository/JpaRepository.java | 15 +++++++++++++++ .../repository/support/SimpleJpaRepository.java | 13 +++++++++++++ .../AbstractPersistableIntegrationTests.java | 13 +++++++++++++ .../data/jpa/repository/UserRepositoryTests.java | 10 ++++++++++ 4 files changed, 51 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 3cd2b165af..5d8afb2689 100644 --- a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java @@ -32,6 +32,7 @@ * @author Christoph Strobl * @author Mark Paluch * @author Sander Krabbenborg + * @author Jesse Wouters */ @NoRepositoryBean public interface JpaRepository extends PagingAndSortingRepository, QueryByExampleExecutor { @@ -131,9 +132,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 1ae36e328e..2f3af6f25e 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 @@ -74,6 +74,7 @@ * @author David Madden * @author Moritz Becker * @author Sander Krabbenborg + * @author Jesse Wouters * @param the type of the entity to handle * @param the type of the entity's identifier */ @@ -323,6 +324,7 @@ protected QueryHints getQueryHints() { * (non-Javadoc) * @see org.springframework.data.jpa.repository.JpaRepository#getOne(java.io.Serializable) */ + @Deprecated @Override public T getOne(ID id) { @@ -330,6 +332,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 50f4d1ba38..1931937673 100644 --- a/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java @@ -36,6 +36,7 @@ * @author Thomas Darimont * @author Oliver Gierke * @author Jens Schauder + * @author Jesse Wouters */ @Transactional @ExtendWith(SpringExtension.class) @@ -66,4 +67,16 @@ void equalsWorksForProxiedEntities() { assertThat(proxy).isEqualTo(proxy); } + + @Test // gh-1697 + 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 d891af5015..ce28bbfd3d 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 Jens Schauder * @author Andrey Kovalev * @author Sander Krabbenborg + * @author Jesse Wouters */ @ExtendWith(SpringExtension.class) @ContextConfiguration("classpath:application-context.xml") @@ -999,6 +1000,15 @@ void looksUpEntityReference() { assertThat(result).isEqualTo(firstUser); } + @Test // gh-1697 + void looksUpEntityReferenceUsingGetById() { + + flushTestUsers(); + + User result = repository.getById(firstUser.getId()); + assertThat(result).isEqualTo(firstUser); + } + @Test // DATAJPA-415 void invokesQueryWithVarargsParametersCorrectly() {