Skip to content

Commit

Permalink
Introduce JpaRepository.getReferenceById.
Browse files Browse the repository at this point in the history
Introduce a repository method that makes it clear the return is a reference. Deprecate the previous methods.

See #2232.
  • Loading branch information
gregturn committed Jan 4, 2022
1 parent 8da9987 commit 2eab5b1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @author Mark Paluch
* @author Sander Krabbenborg
* @author Jesse Wouters
* @author Greg Turnquist
*/
@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +36,7 @@
* @author Oliver Gierke
* @author Jens Schauder
* @author Jesse Wouters
* @author Greg Turnquist
*/
@Transactional
@ExtendWith(SpringExtension.class)
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2eab5b1

Please sign in to comment.