Skip to content

Commit 096f406

Browse files
JesseW28schauder
authored andcommitted
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
1 parent f4d55d7 commit 096f406

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @author Christoph Strobl
3333
* @author Mark Paluch
3434
* @author Sander Krabbenborg
35+
* @author Jesse Wouters
3536
*/
3637
@NoRepositoryBean
3738
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
@@ -131,9 +132,23 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
131132
* @param id must not be {@literal null}.
132133
* @return a reference to the entity with the given identifier.
133134
* @see EntityManager#getReference(Class, Object) for details on when an exception is thrown.
135+
* @deprecated use {@link JpaRepository#getById(ID)} instead.
134136
*/
137+
@Deprecated
135138
T getOne(ID id);
136139

140+
/**
141+
* Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is
142+
* implemented this is very likely to always return an instance and throw an
143+
* {@link javax.persistence.EntityNotFoundException} on first access. Some of them will reject invalid identifiers
144+
* immediately.
145+
*
146+
* @param id must not be {@literal null}.
147+
* @return a reference to the entity with the given identifier.
148+
* @see EntityManager#getReference(Class, Object) for details on when an exception is thrown.
149+
*/
150+
T getById(ID id);
151+
137152
/*
138153
* (non-Javadoc)
139154
* @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)

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

+13
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
* @author David Madden
7575
* @author Moritz Becker
7676
* @author Sander Krabbenborg
77+
* @author Jesse Wouters
7778
* @param <T> the type of the entity to handle
7879
* @param <ID> the type of the entity's identifier
7980
*/
@@ -323,13 +324,25 @@ protected QueryHints getQueryHints() {
323324
* (non-Javadoc)
324325
* @see org.springframework.data.jpa.repository.JpaRepository#getOne(java.io.Serializable)
325326
*/
327+
@Deprecated
326328
@Override
327329
public T getOne(ID id) {
328330

329331
Assert.notNull(id, ID_MUST_NOT_BE_NULL);
330332
return em.getReference(getDomainClass(), id);
331333
}
332334

335+
/*
336+
* (non-Javadoc)
337+
* @see org.springframework.data.jpa.repository.JpaRepository#getById(java.io.Serializable)
338+
*/
339+
@Override
340+
public T getById(ID id) {
341+
342+
Assert.notNull(id, ID_MUST_NOT_BE_NULL);
343+
return em.getReference(getDomainClass(), id);
344+
}
345+
333346
/*
334347
* (non-Javadoc)
335348
* @see org.springframework.data.repository.CrudRepository#existsById(java.io.Serializable)

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

+13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @author Thomas Darimont
3737
* @author Oliver Gierke
3838
* @author Jens Schauder
39+
* @author Jesse Wouters
3940
*/
4041
@Transactional
4142
@ExtendWith(SpringExtension.class)
@@ -66,4 +67,16 @@ void equalsWorksForProxiedEntities() {
6667

6768
assertThat(proxy).isEqualTo(proxy);
6869
}
70+
71+
@Test // gh-1697
72+
void equalsWorksForProxiedEntitiesUsingGetById() {
73+
74+
CustomAbstractPersistable entity = repository.saveAndFlush(new CustomAbstractPersistable());
75+
76+
em.clear();
77+
78+
CustomAbstractPersistable proxy = repository.getById(entity.getId());
79+
80+
assertThat(proxy).isEqualTo(proxy);
81+
}
6982
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
* @author Jens Schauder
9292
* @author Andrey Kovalev
9393
* @author Sander Krabbenborg
94+
* @author Jesse Wouters
9495
*/
9596
@ExtendWith(SpringExtension.class)
9697
@ContextConfiguration("classpath:application-context.xml")
@@ -999,6 +1000,15 @@ void looksUpEntityReference() {
9991000
assertThat(result).isEqualTo(firstUser);
10001001
}
10011002

1003+
@Test // gh-1697
1004+
void looksUpEntityReferenceUsingGetById() {
1005+
1006+
flushTestUsers();
1007+
1008+
User result = repository.getById(firstUser.getId());
1009+
assertThat(result).isEqualTo(firstUser);
1010+
}
1011+
10021012
@Test // DATAJPA-415
10031013
void invokesQueryWithVarargsParametersCorrectly() {
10041014

0 commit comments

Comments
 (0)