Skip to content

Commit

Permalink
Make singleResultOptional throw jakarta exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Aug 8, 2024
1 parent 0791228 commit aad6af7
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 aad6af7

Please sign in to comment.