File tree 2 files changed +14
-2
lines changed
main/java/org/springframework/data/jdbc/repository
test/java/org/springframework/data/jdbc/repository
2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change 19
19
import java .util .List ;
20
20
import java .util .Optional ;
21
21
22
+ import org .springframework .dao .EmptyResultDataAccessException ;
22
23
import org .springframework .data .jdbc .core .JdbcEntityOperations ;
23
24
import org .springframework .data .jdbc .core .JdbcEntityTemplate ;
24
- import org .springframework .data .jdbc .mapping .model .JdbcPersistentEntity ;
25
25
import org .springframework .data .jdbc .mapping .model .JdbcPersistentEntityInformation ;
26
26
import org .springframework .data .repository .CrudRepository ;
27
27
@@ -76,7 +76,11 @@ public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
76
76
@ Override
77
77
public Optional <T > findById (ID id ) {
78
78
79
- return Optional .ofNullable (entityOperations .findById (id , entityInformation .getJavaType ()));
79
+ try {
80
+ return Optional .of (entityOperations .findById (id , entityInformation .getJavaType ()));
81
+ } catch (EmptyResultDataAccessException ex ) {
82
+ return Optional .empty ();
83
+ }
80
84
}
81
85
82
86
/*
Original file line number Diff line number Diff line change @@ -233,6 +233,14 @@ public void updateMany() {
233
233
.containsExactlyInAnyOrder (entity .getName (), other .getName ());
234
234
}
235
235
236
+ @ Test // DATAJDBC-112
237
+ public void findByIdReturnsEmptyWhenNoneFound () {
238
+
239
+ // NOT saving anything, so DB is empty
240
+
241
+ assertThat (repository .findById (-1L )).isEmpty ();
242
+ }
243
+
236
244
private static DummyEntity createDummyEntity () {
237
245
238
246
DummyEntity entity = new DummyEntity ();
You can’t perform that action at this time.
0 commit comments