Skip to content

Commit

Permalink
DATAJPA-1377 added JpaRepository.getById, deprecated JpaRepository.ge…
Browse files Browse the repository at this point in the history
…tOne

Closes spring-projects#1697
  • Loading branch information
JesseW28 committed Mar 3, 2021
1 parent 7c4cba8 commit 3fb5e7d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Sander Krabbenborg
* @author Jesse Wouters
*/
@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
* @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
*/
Expand Down Expand Up @@ -323,13 +324,25 @@ protected QueryHints getQueryHints() {
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.JpaRepository#getOne(java.io.Serializable)
*/
@Deprecated
@Override
public T getOne(ID id) {

Assert.notNull(id, ID_MUST_NOT_BE_NULL);
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @author Thomas Darimont
* @author Oliver Gierke
* @author Jens Schauder
* @author Jesse Wouters
*/
@Transactional
@ExtendWith(SpringExtension.class)
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
* @author Jens Schauder
* @author Andrey Kovalev
* @author Sander Krabbenborg
* @author Jesse Wouters
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:application-context.xml")
Expand Down Expand Up @@ -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() {

Expand Down

0 comments on commit 3fb5e7d

Please sign in to comment.