Skip to content

Commit

Permalink
Fix Optional result type handling in Spring Data JPA
Browse files Browse the repository at this point in the history
Fixes: #42239
  • Loading branch information
geoand committed Jul 31, 2024
1 parent 5337469 commit eab22c3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected void generateFindQueryResultHandling(MethodCreator methodCreator, Resu
if (customResultType == null) {
ResultHandle casted = tryBlock.checkCast(singleResult, entityClassInfo.name().toString());
ResultHandle optional = tryBlock.invokeStaticMethod(
MethodDescriptor.ofMethod(Optional.class, "of", Optional.class, Object.class),
MethodDescriptor.ofMethod(Optional.class, "ofNullable", Optional.class, Object.class),
casted);
tryBlock.returnValue(optional);
} else {
Expand All @@ -134,7 +134,7 @@ protected void generateFindQueryResultHandling(MethodCreator methodCreator, Resu
originalResultType),
singleResult);
ResultHandle optional = tryBlock.invokeStaticMethod(
MethodDescriptor.ofMethod(Optional.class, "of", Optional.class, Object.class),
MethodDescriptor.ofMethod(Optional.class, "ofNullable", Optional.class, Object.class),
customResult);
tryBlock.returnValue(optional);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package io.quarkus.spring.data.deployment;

import java.util.Optional;

import org.springframework.data.repository.ListCrudRepository;

public interface BookListCrudRepository extends ListCrudRepository<Book, Integer> {

Optional<Book> findFirstByNameOrderByBid(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public void shouldSaveBooks() {
"Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Globet of Fire");
}

@Test
@Transactional
public void optionalWithNonExisting() {
assertThat(repo.findFirstByNameOrderByBid("foobar")).isEmpty();
}

private Book populateBook(Integer id, String title) {
Book book = new Book();
book.setBid(id);
Expand Down

0 comments on commit eab22c3

Please sign in to comment.