Adds JpaRepository.getById, deprecats JpaRepository.getOne
This makes the method names in JpaRepository consistent with the names in CrudRepository. Closes #1697 Original pull request #2169
This commit is contained in:
committed by
Jens Schauder
parent
f4d55d7369
commit
096f406508
@@ -32,6 +32,7 @@ import org.springframework.data.repository.query.QueryByExampleExecutor;
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @author Sander Krabbenborg
|
||||
* @author Jesse Wouters
|
||||
*/
|
||||
@NoRepositoryBean
|
||||
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
|
||||
@@ -131,9 +132,23 @@ 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
|
||||
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)
|
||||
|
||||
@@ -74,6 +74,7 @@ import org.springframework.util.Assert;
|
||||
* @author David Madden
|
||||
* @author Moritz Becker
|
||||
* @author Sander Krabbenborg
|
||||
* @author Jesse Wouters
|
||||
* @param <T> the type of the entity to handle
|
||||
* @param <ID> the type of the entity's identifier
|
||||
*/
|
||||
@@ -323,6 +324,7 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
|
||||
* (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 class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
|
||||
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)
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* @author Thomas Darimont
|
||||
* @author Oliver Gierke
|
||||
* @author Jens Schauder
|
||||
* @author Jesse Wouters
|
||||
*/
|
||||
@Transactional
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@@ -66,4 +67,16 @@ public class AbstractPersistableIntegrationTests {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,7 @@ import com.google.common.base.Optional;
|
||||
* @author Jens Schauder
|
||||
* @author Andrey Kovalev
|
||||
* @author Sander Krabbenborg
|
||||
* @author Jesse Wouters
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration("classpath:application-context.xml")
|
||||
@@ -999,6 +1000,15 @@ public class UserRepositoryTests {
|
||||
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() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user