From 82518862ae8211f875fc877ed1e3066619c9214d 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