From 23dc51d2913526f4e89b2e710ba7c2da70b8c930 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 11 Apr 2024 19:07:50 +0200 Subject: [PATCH] fix exceptions thrown from by-id @Find method previously, for SS it would just return null Signed-off-by: Gavin King --- .../processor/annotation/IdFinderMethod.java | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/IdFinderMethod.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/IdFinderMethod.java index ae98a01b0a..e192d35f58 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/IdFinderMethod.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/IdFinderMethod.java @@ -55,7 +55,7 @@ public class IdFinderMethod extends AbstractFinderMethod { @Override boolean singleResult() { - return true; + return false; // we don't need to convert Query exceptions } @Override @@ -67,18 +67,65 @@ public class IdFinderMethod extends AbstractFinderMethod { if ( paramName != null && !isPrimitive(paramType) ) { nullCheck( declaration, paramName ); } - tryReturn( declaration ); + varOrReturn( declaration ); if ( fetchProfiles.isEmpty() ) { findWithNoFetchProfiles( declaration ); } else { findWithFetchProfiles( declaration ); } + throwIfNull( declaration ); convertExceptions( declaration ); closingBrace( declaration ); return declaration.toString(); } + private void throwIfNull(StringBuilder declaration) { + if ( isUsingStatelessSession() ) { + if (dataRepository) { + declaration + .append("\t\tif (_result == null) throw new ") + .append(annotationMetaEntity.importType("jakarta.data.exceptions.EmptyResultException")) + .append("(new ") + .append(annotationMetaEntity.importType("org.hibernate.ObjectNotFoundException")) + .append("((Object) ") + .append(paramName) + .append(", \"") + .append(entity) + .append("\"));\n") + .append("\t\treturn _result;\n"); + } + else { + declaration + .append("\tif (_result == null) throw new ") + .append(annotationMetaEntity.importType("org.hibernate.ObjectNotFoundException")) + .append("((Object) ") + .append(paramName) + .append(", \"") + .append(entity) + .append("\");\n") + .append("\treturn _result;\n"); + } + } + } + + private void varOrReturn(StringBuilder declaration) { + if (dataRepository) { + declaration + .append("\ttry {\n"); + } + if ( isUsingStatelessSession() ) { + declaration + .append("\t\tvar _result = "); + } + else { + declaration + .append("\t\treturn "); + } + declaration + .append(sessionName); + } + private void findWithFetchProfiles(StringBuilder declaration) { unwrapSession( declaration ); declaration