Skip to content

Commit d942361

Browse files
authored
Merge pull request #42241 from geoand/#42239
Fix Optional result type handling in Spring Data JPA
2 parents e44a143 + eab22c3 commit d942361

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/AbstractMethodsAdder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected void generateFindQueryResultHandling(MethodCreator methodCreator, Resu
124124
if (customResultType == null) {
125125
ResultHandle casted = tryBlock.checkCast(singleResult, entityClassInfo.name().toString());
126126
ResultHandle optional = tryBlock.invokeStaticMethod(
127-
MethodDescriptor.ofMethod(Optional.class, "of", Optional.class, Object.class),
127+
MethodDescriptor.ofMethod(Optional.class, "ofNullable", Optional.class, Object.class),
128128
casted);
129129
tryBlock.returnValue(optional);
130130
} else {
@@ -134,7 +134,7 @@ protected void generateFindQueryResultHandling(MethodCreator methodCreator, Resu
134134
originalResultType),
135135
singleResult);
136136
ResultHandle optional = tryBlock.invokeStaticMethod(
137-
MethodDescriptor.ofMethod(Optional.class, "of", Optional.class, Object.class),
137+
MethodDescriptor.ofMethod(Optional.class, "ofNullable", Optional.class, Object.class),
138138
customResult);
139139
tryBlock.returnValue(optional);
140140
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package io.quarkus.spring.data.deployment;
22

3+
import java.util.Optional;
4+
35
import org.springframework.data.repository.ListCrudRepository;
46

57
public interface BookListCrudRepository extends ListCrudRepository<Book, Integer> {
8+
9+
Optional<Book> findFirstByNameOrderByBid(String name);
610
}

extensions/spring-data-jpa/deployment/src/test/java/io/quarkus/spring/data/deployment/BookListCrudRepositoryTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public void shouldSaveBooks() {
6969
"Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Globet of Fire");
7070
}
7171

72+
@Test
73+
@Transactional
74+
public void optionalWithNonExisting() {
75+
assertThat(repo.findFirstByNameOrderByBid("foobar")).isEmpty();
76+
}
77+
7278
private Book populateBook(Integer id, String title) {
7379
Book book = new Book();
7480
book.setBid(id);

0 commit comments

Comments
 (0)