HHH-17772 Jakarta exception conversion for @Query methods

This commit is contained in:
Gavin King 2024-02-23 23:49:33 +01:00
parent 25d7cc8681
commit 3539551305
7 changed files with 87 additions and 38 deletions

View File

@ -20,7 +20,6 @@ import static org.hibernate.jpamodelgen.util.StringUtil.getUpperUnderscoreCaseFr
public abstract class AbstractFinderMethod extends AbstractQueryMethod { public abstract class AbstractFinderMethod extends AbstractQueryMethod {
final String entity; final String entity;
final List<String> fetchProfiles; final List<String> fetchProfiles;
final boolean convertToDataExceptions;
public AbstractFinderMethod( public AbstractFinderMethod(
AnnotationMetaEntity annotationMetaEntity, AnnotationMetaEntity annotationMetaEntity,
@ -38,10 +37,10 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
methodName, methodName,
paramNames, paramTypes, entity, paramNames, paramTypes, entity,
sessionType, sessionName, sessionType, sessionName,
belongsToDao, addNonnullAnnotation ); belongsToDao, addNonnullAnnotation,
convertToDataExceptions );
this.entity = entity; this.entity = entity;
this.fetchProfiles = fetchProfiles; this.fetchProfiles = fetchProfiles;
this.convertToDataExceptions = convertToDataExceptions;
} }
@Override @Override
@ -178,7 +177,7 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
parameters( paramTypes, declaration ) ; parameters( paramTypes, declaration ) ;
declaration declaration
.append(" {\n"); .append(" {\n");
if (convertToDataExceptions) { if (dataRepository) {
declaration declaration
.append("\ttry {\n\t"); .append("\ttry {\n\t");
} }
@ -187,27 +186,6 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
.append(sessionName); .append(sessionName);
} }
void convertExceptions(StringBuilder declaration) {
if (convertToDataExceptions) {
declaration
.append("\t}\n")
.append("\tcatch (")
.append(annotationMetaEntity.importType("jakarta.persistence.NoResultException"))
.append(" exception) {\n")
.append("\t\tthrow new ")
.append(annotationMetaEntity.importType("jakarta.data.exceptions.EmptyResultException"))
.append("(exception);\n")
.append("\t}\n")
.append("\tcatch (")
.append(annotationMetaEntity.importType("jakarta.persistence.NonUniqueResultException"))
.append(" exception) {\n")
.append("\t\tthrow new ")
.append(annotationMetaEntity.importType("jakarta.data.exceptions.NonUniqueResultException"))
.append("(exception);\n")
.append("\t}\n");
}
}
private void entityType(StringBuilder declaration) { private void entityType(StringBuilder declaration) {
if ( isReactive() ) { if ( isReactive() ) {
declaration declaration

View File

@ -38,6 +38,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
final String sessionName; final String sessionName;
final boolean belongsToDao; final boolean belongsToDao;
final boolean addNonnullAnnotation; final boolean addNonnullAnnotation;
final boolean dataRepository;
public AbstractQueryMethod( public AbstractQueryMethod(
AnnotationMetaEntity annotationMetaEntity, AnnotationMetaEntity annotationMetaEntity,
@ -47,7 +48,8 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
String sessionType, String sessionType,
String sessionName, String sessionName,
boolean belongsToDao, boolean belongsToDao,
boolean addNonnullAnnotation) { boolean addNonnullAnnotation,
boolean dataRepository) {
this.annotationMetaEntity = annotationMetaEntity; this.annotationMetaEntity = annotationMetaEntity;
this.methodName = methodName; this.methodName = methodName;
this.paramNames = paramNames; this.paramNames = paramNames;
@ -57,6 +59,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
this.sessionName = sessionName; this.sessionName = sessionName;
this.belongsToDao = belongsToDao; this.belongsToDao = belongsToDao;
this.addNonnullAnnotation = addNonnullAnnotation; this.addNonnullAnnotation = addNonnullAnnotation;
this.dataRepository = dataRepository;
} }
@Override @Override
@ -272,6 +275,40 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
return true; return true;
} }
void convertExceptions(StringBuilder declaration) {
if (dataRepository) {
declaration
.append("\t}\n");
if ( singleResult() ) {
declaration
.append("\tcatch (")
.append(annotationMetaEntity.importType("jakarta.persistence.NoResultException"))
.append(" exception) {\n")
.append("\t\tthrow new ")
.append(annotationMetaEntity.importType("jakarta.data.exceptions.EmptyResultException"))
.append("(exception);\n")
.append("\t}\n")
.append("\tcatch (")
.append(annotationMetaEntity.importType("jakarta.persistence.NonUniqueResultException"))
.append(" exception) {\n")
.append("\t\tthrow new ")
.append(annotationMetaEntity.importType("jakarta.data.exceptions.NonUniqueResultException"))
.append("(exception);\n")
.append("\t}\n");
}
declaration
.append("\tcatch (")
.append(annotationMetaEntity.importType("jakarta.persistence.PersistenceException"))
.append(" exception) {\n")
.append("\t\tthrow new ")
.append(annotationMetaEntity.importType("jakarta.data.exceptions.DataException"))
.append("(exception);\n")
.append("\t}\n");
}
}
abstract boolean singleResult();
@Nullable String getSortableEntityClass() { @Nullable String getSortableEntityClass() {
return returnTypeName; return returnTypeName;
} }

View File

@ -1209,7 +1209,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final List<String> paramTypes = parameterTypes( method ); final List<String> paramTypes = parameterTypes( method );
final String[] sessionType = sessionTypeFromParameters( paramNames, paramTypes ); final String[] sessionType = sessionTypeFromParameters( paramNames, paramTypes );
if ( returnType != null && returnType.getKind() == TypeKind.DECLARED ) { if ( returnType != null && returnType.getKind() == TypeKind.DECLARED ) {
if ( !((DeclaredType) returnType).getTypeArguments().isEmpty() ) { final DeclaredType declaredType = (DeclaredType) returnType;
if ( !declaredType.getTypeArguments().isEmpty() ) {
context.message( method, mirror, value, context.message( method, mirror, value,
"query result type may not be a generic type" "query result type may not be a generic type"
+ " (change '" + returnType + + " (change '" + returnType +
@ -1231,7 +1232,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
dao, dao,
sessionType[0], sessionType[0],
sessionType[1], sessionType[1],
context.addNonnullAnnotation() context.addNonnullAnnotation(),
dataRepository
); );
putMember( attribute.getPropertyName() + paramTypes, attribute ); putMember( attribute.getPropertyName() + paramTypes, attribute );

View File

@ -39,7 +39,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
boolean dataRepository) { boolean dataRepository) {
super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles, super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, addNonnullAnnotation, paramNames, paramTypes, addNonnullAnnotation,
dataRepository && containerType == null ); dataRepository );
this.containerType = containerType; this.containerType = containerType;
this.paramNullability = paramNullability; this.paramNullability = paramNullability;
this.orderBys = orderBys; this.orderBys = orderBys;
@ -50,6 +50,11 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
return paramNullability.get(index); return paramNullability.get(index);
} }
@Override
boolean singleResult() {
return containerType == null;
}
@Override @Override
public String getAttributeDeclarationString() { public String getAttributeDeclarationString() {
final List<String> paramTypes = parameterTypes(); final List<String> paramTypes = parameterTypes();
@ -77,7 +82,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
private void executeQuery(StringBuilder declaration, List<String> paramTypes) { private void executeQuery(StringBuilder declaration, List<String> paramTypes) {
declaration declaration
.append('\n'); .append('\n');
if (convertToDataExceptions) { if (dataRepository) {
declaration declaration
.append("\ttry {\n\t"); .append("\ttry {\n\t");
} }
@ -126,7 +131,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
} }
declaration declaration
.append(';'); .append(';');
if (convertToDataExceptions) { if (dataRepository) {
declaration declaration
.append('\n'); .append('\n');
} }

View File

@ -44,6 +44,11 @@ public class IdFinderMethod extends AbstractFinderMethod {
return false; return false;
} }
@Override
boolean singleResult() {
return true;
}
@Override @Override
public String getAttributeDeclarationString() { public String getAttributeDeclarationString() {
final StringBuilder declaration = new StringBuilder(); final StringBuilder declaration = new StringBuilder();
@ -71,7 +76,7 @@ public class IdFinderMethod extends AbstractFinderMethod {
.append("\n\t\t\t.load(") .append("\n\t\t\t.load(")
.append(paramName) .append(paramName)
.append(");"); .append(");");
if (convertToDataExceptions) { if (dataRepository) {
declaration declaration
.append("\n"); .append("\n");
} }
@ -84,7 +89,7 @@ public class IdFinderMethod extends AbstractFinderMethod {
.append(".class, ") .append(".class, ")
.append(paramName) .append(paramName)
.append(");"); .append(");");
if (convertToDataExceptions) { if (dataRepository) {
declaration declaration
.append("\n"); .append("\n");
} }

View File

@ -37,6 +37,11 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
return paramNullability.get(index); return paramNullability.get(index);
} }
@Override
boolean singleResult() {
return true;
}
@Override @Override
public String getAttributeDeclarationString() { public String getAttributeDeclarationString() {
final StringBuilder declaration = new StringBuilder(); final StringBuilder declaration = new StringBuilder();

View File

@ -39,12 +39,14 @@ public class QueryMethod extends AbstractQueryMethod {
boolean belongsToDao, boolean belongsToDao,
String sessionType, String sessionType,
String sessionName, String sessionName,
boolean addNonnullAnnotation) { boolean addNonnullAnnotation,
boolean dataRepository) {
super( annotationMetaEntity, super( annotationMetaEntity,
methodName, methodName,
paramNames, paramTypes, returnTypeName, paramNames, paramTypes, returnTypeName,
sessionType, sessionName, sessionType, sessionName,
belongsToDao, addNonnullAnnotation ); belongsToDao, addNonnullAnnotation,
dataRepository );
this.queryString = queryString; this.queryString = queryString;
this.containerTypeName = containerTypeName; this.containerTypeName = containerTypeName;
this.isUpdate = isUpdate; this.isUpdate = isUpdate;
@ -66,6 +68,11 @@ public class QueryMethod extends AbstractQueryMethod {
return true; return true;
} }
@Override
boolean singleResult() {
return containerTypeName == null;
}
@Override @Override
public String getAttributeDeclarationString() { public String getAttributeDeclarationString() {
final List<String> paramTypes = parameterTypes(); final List<String> paramTypes = parameterTypes();
@ -79,8 +86,13 @@ public class QueryMethod extends AbstractQueryMethod {
.append(methodName); .append(methodName);
parameters( paramTypes, declaration ); parameters( paramTypes, declaration );
declaration declaration
.append(" {") .append(" {\n");
.append("\n\t"); if (dataRepository) {
declaration
.append("\ttry {\n\t");
}
declaration
.append("\t");
if ( returnTypeName == null || !returnTypeName.equals("void") ) { if ( returnTypeName == null || !returnTypeName.equals("void") ) {
declaration declaration
.append("return "); .append("return ");
@ -107,7 +119,8 @@ public class QueryMethod extends AbstractQueryMethod {
declaration.append(")"); declaration.append(")");
boolean unwrapped = setParameters( paramTypes, declaration ); boolean unwrapped = setParameters( paramTypes, declaration );
execute( declaration, unwrapped ); execute( declaration, unwrapped );
declaration.append(";\n}"); convertExceptions( declaration );
declaration.append("\n}");
return declaration.toString(); return declaration.toString();
} }
@ -138,6 +151,10 @@ public class QueryMethod extends AbstractQueryMethod {
} }
} }
declaration.append(';');
if (dataRepository) {
declaration.append('\n');
}
} }
private boolean setParameters(List<String> paramTypes, StringBuilder declaration) { private boolean setParameters(List<String> paramTypes, StringBuilder declaration) {