Skip to content

Commit

Permalink
DATAJPA-1377 - Add JpaRepository.getById, deprecate JpaRepository.getOne
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseW28 committed Mar 3, 2021
1 parent ab4dd9c commit c88abce
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
* @author Jesse Wouters
*/
@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
Expand Down Expand Up @@ -121,9 +122,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 @@ -73,6 +73,7 @@
* @author Jens Schauder
* @author David Madden
* @author Moritz Becker
* @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 @@ -322,13 +323,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 @@ -25,6 +25,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.AbstractPersistable;
import org.springframework.data.jpa.domain.sample.CustomAbstractPersistable;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.repository.sample.CustomAbstractPersistableRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
Expand All @@ -36,6 +37,7 @@
* @author Thomas Darimont
* @author Oliver Gierke
* @author Jens Schauder
* @author Jesse Wouters
*/
@Transactional
@ExtendWith(SpringExtension.class)
Expand Down Expand Up @@ -66,4 +68,16 @@ void equalsWorksForProxiedEntities() {

assertThat(proxy).isEqualTo(proxy);
}

@Test // gh-1679
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 Kevin Peters
* @author Jens Schauder
* @author Andrey Kovalev
* @author Jesse Wouters
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:application-context.xml")
Expand Down Expand Up @@ -987,6 +988,15 @@ void looksUpEntityReference() {
assertThat(result).isEqualTo(firstUser);
}

@Test // gh-1679
void looksUpEntityReferenceUsingGetById() {

flushTestUsers();

User result = repository.getById(firstUser.getId());
assertThat(result).isEqualTo(firstUser);
}

@Test // DATAJPA-415
void invokesQueryWithVarargsParametersCorrectly() {

Expand Down

0 comments on commit c88abce

Please sign in to comment.