fix exceptions thrown from by-id @Find method

previously, for SS it would just return null

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-04-11 19:07:50 +02:00 committed by Christian Beikov
parent 0c6d3595a8
commit 82518862ae
1 changed files with 49 additions and 2 deletions

View File

@ -55,7 +55,7 @@ public class IdFinderMethod extends AbstractFinderMethod {
@Override @Override
boolean singleResult() { boolean singleResult() {
return true; return false; // we don't need to convert Query exceptions
} }
@Override @Override
@ -67,18 +67,65 @@ public class IdFinderMethod extends AbstractFinderMethod {
if ( paramName != null && !isPrimitive(paramType) ) { if ( paramName != null && !isPrimitive(paramType) ) {
nullCheck( declaration, paramName ); nullCheck( declaration, paramName );
} }
tryReturn( declaration ); varOrReturn( declaration );
if ( fetchProfiles.isEmpty() ) { if ( fetchProfiles.isEmpty() ) {
findWithNoFetchProfiles( declaration ); findWithNoFetchProfiles( declaration );
} }
else { else {
findWithFetchProfiles( declaration ); findWithFetchProfiles( declaration );
} }
throwIfNull( declaration );
convertExceptions( declaration ); convertExceptions( declaration );
closingBrace( declaration ); closingBrace( declaration );
return declaration.toString(); 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) { private void findWithFetchProfiles(StringBuilder declaration) {
unwrapSession( declaration ); unwrapSession( declaration );
declaration declaration