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
This commit is contained in:
Greg L. Turnquist
2022-01-04 09:18:56 -06:00
committed by Jens Schauder
parent 8c4123f6b2
commit 0926d4bfb6
4 changed files with 57 additions and 11 deletions

View File

@@ -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<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
@@ -96,7 +97,9 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
* @deprecated Use {@link #deleteAllInBatch(Iterable)} instead.
*/
@Deprecated
default void deleteInBatch(Iterable<T> entities){deleteAllInBatch(entities);}
default void deleteInBatch(Iterable<T> 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<T, ID> extends PagingAndSortingRepository<T, ID>,
*/
void deleteAllInBatch(Iterable<T> 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<T, ID> extends PagingAndSortingRepository<T, ID>,
* @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<T, ID> extends PagingAndSortingRepository<T, ID>,
* @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)

View File

@@ -331,26 +331,34 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
@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) {

View File

@@ -21,7 +21,6 @@ import javax.persistence.EntityManager;
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 @@ import org.springframework.transaction.annotation.Transactional;
* @author Oliver Gierke
* @author Jens Schauder
* @author Jesse Wouters
* @author Greg Turnquist
*/
@Transactional
@ExtendWith(SpringExtension.class)
@@ -79,4 +79,16 @@ public class AbstractPersistableIntegrationTests {
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);
}
}

View File

@@ -1011,7 +1011,16 @@ public class UserRepositoryTests {
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();