Skip to content

Commit 2eab5b1

Browse files
committed
Introduce JpaRepository.getReferenceById.
Introduce a repository method that makes it clear the return is a reference. Deprecate the previous methods. See #2232.
1 parent 8da9987 commit 2eab5b1

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

src/main/java/org/springframework/data/jpa/repository/JpaRepository.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* @author Mark Paluch
3434
* @author Sander Krabbenborg
3535
* @author Jesse Wouters
36+
* @author Greg Turnquist
3637
*/
3738
@NoRepositoryBean
3839
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
@@ -96,7 +97,9 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
9697
* @deprecated Use {@link #deleteAllInBatch(Iterable)} instead.
9798
*/
9899
@Deprecated
99-
default void deleteInBatch(Iterable<T> entities){deleteAllInBatch(entities);}
100+
default void deleteInBatch(Iterable<T> entities) {
101+
deleteAllInBatch(entities);
102+
}
100103

101104
/**
102105
* 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>,
108111
*/
109112
void deleteAllInBatch(Iterable<T> entities);
110113

111-
112114
/**
113115
* Deletes the entities identified by the given ids using a single query. This kind of operation leaves JPAs first
114116
* level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this method.
@@ -146,10 +148,25 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
146148
* @param id must not be {@literal null}.
147149
* @return a reference to the entity with the given identifier.
148150
* @see EntityManager#getReference(Class, Object) for details on when an exception is thrown.
151+
* @deprecated use {@link JpaRepository#getReferenceById(ID)} instead.
149152
* @since 2.5
150153
*/
154+
@Deprecated
151155
T getById(ID id);
152156

157+
/**
158+
* Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is
159+
* implemented this is very likely to always return an instance and throw an
160+
* {@link javax.persistence.EntityNotFoundException} on first access. Some of them will reject invalid identifiers
161+
* immediately.
162+
*
163+
* @param id must not be {@literal null}.
164+
* @return a reference to the entity with the given identifier.
165+
* @see EntityManager#getReference(Class, Object) for details on when an exception is thrown.
166+
* @since 2.7
167+
*/
168+
T getReferenceById(ID id);
169+
153170
/*
154171
* (non-Javadoc)
155172
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)

src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -331,26 +331,34 @@ protected QueryHints getQueryHints() {
331331
@Deprecated
332332
@Override
333333
public T getOne(ID id) {
334-
335-
Assert.notNull(id, ID_MUST_NOT_BE_NULL);
336-
return em.getReference(getDomainClass(), id);
334+
return getReferenceById(id);
337335
}
338336

339337
/*
340338
* (non-Javadoc)
341339
* @see org.springframework.data.jpa.repository.JpaRepository#getById(java.io.Serializable)
342340
*/
341+
@Deprecated
343342
@Override
344343
public T getById(ID id) {
344+
return getReferenceById(id);
345+
}
346+
347+
/*
348+
* (non-Javadoc)
349+
* @see org.springframework.data.jpa.repository.JpaRepository#getReferenceById(java.io.Serializable)
350+
*/
351+
@Override
352+
public T getReferenceById(ID id) {
345353

346354
Assert.notNull(id, ID_MUST_NOT_BE_NULL);
347355
return em.getReference(getDomainClass(), id);
348356
}
349357

350358
/*
351-
* (non-Javadoc)
352-
* @see org.springframework.data.repository.CrudRepository#existsById(java.io.Serializable)
353-
*/
359+
* (non-Javadoc)
360+
* @see org.springframework.data.repository.CrudRepository#existsById(java.io.Serializable)
361+
*/
354362
@Override
355363
public boolean existsById(ID id) {
356364

src/test/java/org/springframework/data/jpa/repository/AbstractPersistableIntegrationTests.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.junit.jupiter.api.Test;
2323
import org.junit.jupiter.api.extension.ExtendWith;
24-
2524
import org.springframework.beans.factory.annotation.Autowired;
2625
import org.springframework.data.jpa.domain.AbstractPersistable;
2726
import org.springframework.data.jpa.domain.sample.CustomAbstractPersistable;
@@ -37,6 +36,7 @@
3736
* @author Oliver Gierke
3837
* @author Jens Schauder
3938
* @author Jesse Wouters
39+
* @author Greg Turnquist
4040
*/
4141
@Transactional
4242
@ExtendWith(SpringExtension.class)
@@ -79,4 +79,16 @@ void equalsWorksForProxiedEntitiesUsingGetById() {
7979

8080
assertThat(proxy).isEqualTo(proxy);
8181
}
82+
83+
@Test // gh-1697
84+
void equalsWorksForProxiedEntitiesUsingGetReferenceById() {
85+
86+
CustomAbstractPersistable entity = repository.saveAndFlush(new CustomAbstractPersistable());
87+
88+
em.clear();
89+
90+
CustomAbstractPersistable proxy = repository.getReferenceById(entity.getId());
91+
92+
assertThat(proxy).isEqualTo(proxy);
93+
}
8294
}

src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,16 @@ void looksUpEntityReferenceUsingGetById() {
10111011
assertThat(result).isEqualTo(firstUser);
10121012
}
10131013

1014-
@Test // DATAJPA-415
1014+
@Test // gh-1697
1015+
void looksUpEntityReferenceUsingGetReferenceById() {
1016+
1017+
flushTestUsers();
1018+
1019+
User result = repository.getReferenceById(firstUser.getId());
1020+
assertThat(result).isEqualTo(firstUser);
1021+
}
1022+
1023+
@Test // DATAJPA-415
10151024
void invokesQueryWithVarargsParametersCorrectly() {
10161025

10171026
flushTestUsers();

0 commit comments

Comments
 (0)