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 cbc36e210be..f8b9a06c7a3 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 @@ * @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. @@ -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 5f8a82715ad..0fbc14f20e0 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 @@ protected QueryHints getQueryHints() { @Deprecated @Override public T getOne(ID id) { - - Assert.notNull(id, ID_MUST_NOT_BE_NULL); - return em.getReference(getDomainClass(), id); + return getReferenceById(id); } /* * (non-Javadoc) * @see org.springframework.data.jpa.repository.JpaRepository#getById(java.io.Serializable) */ + @Deprecated @Override public T getById(ID id) { + return getReferenceById(id); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.jpa.repository.JpaRepository#getReferenceById(java.io.Serializable) + */ + @Override + public T getReferenceById(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) - */ + * (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#existsById(java.io.Serializable) + */ @Override public boolean existsById(ID id) { 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 19319376730..2f9dadf2b38 100644 --- a/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java @@ -21,7 +21,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.domain.AbstractPersistable; import org.springframework.data.jpa.domain.sample.CustomAbstractPersistable; @@ -37,6 +36,7 @@ * @author Oliver Gierke * @author Jens Schauder * @author Jesse Wouters + * @author Greg Turnquist */ @Transactional @ExtendWith(SpringExtension.class) @@ -79,4 +79,16 @@ void equalsWorksForProxiedEntitiesUsingGetById() { assertThat(proxy).isEqualTo(proxy); } + + @Test // gh-1697 + void equalsWorksForProxiedEntitiesUsingGetReferenceById() { + + CustomAbstractPersistable entity = repository.saveAndFlush(new CustomAbstractPersistable()); + + em.clear(); + + CustomAbstractPersistable proxy = repository.getReferenceById(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 a4a438e8572..113ae1a5ad9 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -1011,7 +1011,16 @@ void looksUpEntityReferenceUsingGetById() { assertThat(result).isEqualTo(firstUser); } - @Test // DATAJPA-415 + @Test // gh-1697 + void looksUpEntityReferenceUsingGetReferenceById() { + + flushTestUsers(); + + User result = repository.getReferenceById(firstUser.getId()); + assertThat(result).isEqualTo(firstUser); + } + + @Test // DATAJPA-415 void invokesQueryWithVarargsParametersCorrectly() { flushTestUsers();