Skip to content

Commit

Permalink
Merge pull request #42423 from marko-bekhta/fix/panache-single-result…
Browse files Browse the repository at this point in the history
…-exception

Make singleResultOptional throw jakarta exceptions
  • Loading branch information
gastaldi authored Aug 9, 2024
2 parents 4ad4bba + 55b1565 commit a2de5a0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@ public <T extends Entity> T singleResult() {
public <T extends Entity> Optional<T> singleResultOptional() {
SelectionQuery hibernateQuery = createQuery();
try (NonThrowingCloseable c = applyFilters()) {
return hibernateQuery.uniqueResultOptional();
// Yes, there's a much nicer hibernateQuery.uniqueResultOptional() BUT
// it throws org.hibernate.NonUniqueResultException instead of a jakarta.persistence.NonUniqueResultException
// and at this point changing it would be a breaking change >_<
return Optional.ofNullable((T) hibernateQuery.getSingleResultOrNull());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1860,4 +1860,41 @@ public String testBug31117() {
Assertions.assertEquals(1, Person.delete("\r\n \n\ndelete\nfrom\n Person2\nwhere\nname = ?1", "foo"));
return "OK";
}

@GET
@Path("42416")
public String testBug42416() {
createSomeEntities42416();
runSomeTests42416();
return "OK";
}

@Transactional
public void createSomeEntities42416() {
Fruit.deleteAll();
Fruit f = new Fruit("apple", "red");
f.persist();

Fruit f2 = new Fruit("apple", "yellow");
f2.persist();
}

@Transactional
public void runSomeTests42416() {
try {
Fruit.find("where name = ?1", "apple").singleResult();
} catch (jakarta.persistence.NonUniqueResultException e) {
// all good let's continue
}
try {
Fruit.find("where name = ?1", "not-a-fruit").singleResult();
} catch (jakarta.persistence.NoResultException e) {
// all good let's continue
}
try {
Fruit.find("where name = ?1", "apple").singleResultOptional();
} catch (jakarta.persistence.NonUniqueResultException e) {
// all good let's continue
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,9 @@ public void testBug36496() {
public void testBug31117() {
RestAssured.when().get("/test/31117").then().body(is("OK"));
}

@Test
public void testBug42416() {
RestAssured.when().get("/test/42416").then().body(is("OK"));
}
}

0 comments on commit a2de5a0

Please sign in to comment.