Skip to content

Commit 55b1565

Browse files
committed
Make singleResultOptional throw jakarta exceptions
1 parent 4ad4bba commit 55b1565

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,10 @@ public <T extends Entity> T singleResult() {
337337
public <T extends Entity> Optional<T> singleResultOptional() {
338338
SelectionQuery hibernateQuery = createQuery();
339339
try (NonThrowingCloseable c = applyFilters()) {
340-
return hibernateQuery.uniqueResultOptional();
340+
// Yes, there's a much nicer hibernateQuery.uniqueResultOptional() BUT
341+
// it throws org.hibernate.NonUniqueResultException instead of a jakarta.persistence.NonUniqueResultException
342+
// and at this point changing it would be a breaking change >_<
343+
return Optional.ofNullable((T) hibernateQuery.getSingleResultOrNull());
341344
}
342345
}
343346

integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/defaultpu/TestEndpoint.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,4 +1860,41 @@ public String testBug31117() {
18601860
Assertions.assertEquals(1, Person.delete("\r\n \n\ndelete\nfrom\n Person2\nwhere\nname = ?1", "foo"));
18611861
return "OK";
18621862
}
1863+
1864+
@GET
1865+
@Path("42416")
1866+
public String testBug42416() {
1867+
createSomeEntities42416();
1868+
runSomeTests42416();
1869+
return "OK";
1870+
}
1871+
1872+
@Transactional
1873+
public void createSomeEntities42416() {
1874+
Fruit.deleteAll();
1875+
Fruit f = new Fruit("apple", "red");
1876+
f.persist();
1877+
1878+
Fruit f2 = new Fruit("apple", "yellow");
1879+
f2.persist();
1880+
}
1881+
1882+
@Transactional
1883+
public void runSomeTests42416() {
1884+
try {
1885+
Fruit.find("where name = ?1", "apple").singleResult();
1886+
} catch (jakarta.persistence.NonUniqueResultException e) {
1887+
// all good let's continue
1888+
}
1889+
try {
1890+
Fruit.find("where name = ?1", "not-a-fruit").singleResult();
1891+
} catch (jakarta.persistence.NoResultException e) {
1892+
// all good let's continue
1893+
}
1894+
try {
1895+
Fruit.find("where name = ?1", "apple").singleResultOptional();
1896+
} catch (jakarta.persistence.NonUniqueResultException e) {
1897+
// all good let's continue
1898+
}
1899+
}
18631900
}

integration-tests/hibernate-orm-panache/src/test/java/io/quarkus/it/panache/defaultpu/PanacheFunctionalityTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,9 @@ public void testBug36496() {
258258
public void testBug31117() {
259259
RestAssured.when().get("/test/31117").then().body(is("OK"));
260260
}
261+
262+
@Test
263+
public void testBug42416() {
264+
RestAssured.when().get("/test/42416").then().body(is("OK"));
265+
}
261266
}

0 commit comments

Comments
 (0)