From 55b156591aa8efadb96bf2710e79e0a8674dc649 Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Thu, 8 Aug 2024 22:06:57 +0200 Subject: [PATCH] Make singleResultOptional throw jakarta exceptions --- .../runtime/CommonPanacheQueryImpl.java | 5 ++- .../it/panache/defaultpu/TestEndpoint.java | 37 +++++++++++++++++++ .../defaultpu/PanacheFunctionalityTest.java | 5 +++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java index 42a14c283900a..12446bb9d0ccc 100644 --- a/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java +++ b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/CommonPanacheQueryImpl.java @@ -337,7 +337,10 @@ public T singleResult() { public Optional 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()); } } diff --git a/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/defaultpu/TestEndpoint.java b/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/defaultpu/TestEndpoint.java index 07b56adcd7898..c6eed8c982a8a 100644 --- a/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/defaultpu/TestEndpoint.java +++ b/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/defaultpu/TestEndpoint.java @@ -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 + } + } } diff --git a/integration-tests/hibernate-orm-panache/src/test/java/io/quarkus/it/panache/defaultpu/PanacheFunctionalityTest.java b/integration-tests/hibernate-orm-panache/src/test/java/io/quarkus/it/panache/defaultpu/PanacheFunctionalityTest.java index 2359770bb2751..823f0f5c1db06 100644 --- a/integration-tests/hibernate-orm-panache/src/test/java/io/quarkus/it/panache/defaultpu/PanacheFunctionalityTest.java +++ b/integration-tests/hibernate-orm-panache/src/test/java/io/quarkus/it/panache/defaultpu/PanacheFunctionalityTest.java @@ -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")); + } }