HHH-17772 Jakarta exception conversion for @Query methods
This commit is contained in:
parent
25d7cc8681
commit
3539551305
|
@ -20,7 +20,6 @@ import static org.hibernate.jpamodelgen.util.StringUtil.getUpperUnderscoreCaseFr
|
|||
public abstract class AbstractFinderMethod extends AbstractQueryMethod {
|
||||
final String entity;
|
||||
final List<String> fetchProfiles;
|
||||
final boolean convertToDataExceptions;
|
||||
|
||||
public AbstractFinderMethod(
|
||||
AnnotationMetaEntity annotationMetaEntity,
|
||||
|
@ -38,10 +37,10 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
|
|||
methodName,
|
||||
paramNames, paramTypes, entity,
|
||||
sessionType, sessionName,
|
||||
belongsToDao, addNonnullAnnotation );
|
||||
belongsToDao, addNonnullAnnotation,
|
||||
convertToDataExceptions );
|
||||
this.entity = entity;
|
||||
this.fetchProfiles = fetchProfiles;
|
||||
this.convertToDataExceptions = convertToDataExceptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -178,7 +177,7 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
|
|||
parameters( paramTypes, declaration ) ;
|
||||
declaration
|
||||
.append(" {\n");
|
||||
if (convertToDataExceptions) {
|
||||
if (dataRepository) {
|
||||
declaration
|
||||
.append("\ttry {\n\t");
|
||||
}
|
||||
|
@ -187,27 +186,6 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
|
|||
.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) {
|
||||
if ( isReactive() ) {
|
||||
declaration
|
||||
|
|
|
@ -38,6 +38,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
|
|||
final String sessionName;
|
||||
final boolean belongsToDao;
|
||||
final boolean addNonnullAnnotation;
|
||||
final boolean dataRepository;
|
||||
|
||||
public AbstractQueryMethod(
|
||||
AnnotationMetaEntity annotationMetaEntity,
|
||||
|
@ -47,7 +48,8 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
|
|||
String sessionType,
|
||||
String sessionName,
|
||||
boolean belongsToDao,
|
||||
boolean addNonnullAnnotation) {
|
||||
boolean addNonnullAnnotation,
|
||||
boolean dataRepository) {
|
||||
this.annotationMetaEntity = annotationMetaEntity;
|
||||
this.methodName = methodName;
|
||||
this.paramNames = paramNames;
|
||||
|
@ -57,6 +59,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
|
|||
this.sessionName = sessionName;
|
||||
this.belongsToDao = belongsToDao;
|
||||
this.addNonnullAnnotation = addNonnullAnnotation;
|
||||
this.dataRepository = dataRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -272,6 +275,40 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
|
|||
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() {
|
||||
return returnTypeName;
|
||||
}
|
||||
|
|
|
@ -1209,7 +1209,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
|||
final List<String> paramTypes = parameterTypes( method );
|
||||
final String[] sessionType = sessionTypeFromParameters( paramNames, paramTypes );
|
||||
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,
|
||||
"query result type may not be a generic type"
|
||||
+ " (change '" + returnType +
|
||||
|
@ -1231,7 +1232,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
|||
dao,
|
||||
sessionType[0],
|
||||
sessionType[1],
|
||||
context.addNonnullAnnotation()
|
||||
context.addNonnullAnnotation(),
|
||||
dataRepository
|
||||
);
|
||||
putMember( attribute.getPropertyName() + paramTypes, attribute );
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
|||
boolean dataRepository) {
|
||||
super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
|
||||
paramNames, paramTypes, addNonnullAnnotation,
|
||||
dataRepository && containerType == null );
|
||||
dataRepository );
|
||||
this.containerType = containerType;
|
||||
this.paramNullability = paramNullability;
|
||||
this.orderBys = orderBys;
|
||||
|
@ -50,6 +50,11 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
|||
return paramNullability.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean singleResult() {
|
||||
return containerType == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAttributeDeclarationString() {
|
||||
final List<String> paramTypes = parameterTypes();
|
||||
|
@ -77,7 +82,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
|||
private void executeQuery(StringBuilder declaration, List<String> paramTypes) {
|
||||
declaration
|
||||
.append('\n');
|
||||
if (convertToDataExceptions) {
|
||||
if (dataRepository) {
|
||||
declaration
|
||||
.append("\ttry {\n\t");
|
||||
}
|
||||
|
@ -126,7 +131,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
|||
}
|
||||
declaration
|
||||
.append(';');
|
||||
if (convertToDataExceptions) {
|
||||
if (dataRepository) {
|
||||
declaration
|
||||
.append('\n');
|
||||
}
|
||||
|
|
|
@ -44,6 +44,11 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean singleResult() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAttributeDeclarationString() {
|
||||
final StringBuilder declaration = new StringBuilder();
|
||||
|
@ -71,7 +76,7 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
|||
.append("\n\t\t\t.load(")
|
||||
.append(paramName)
|
||||
.append(");");
|
||||
if (convertToDataExceptions) {
|
||||
if (dataRepository) {
|
||||
declaration
|
||||
.append("\n");
|
||||
}
|
||||
|
@ -84,7 +89,7 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
|||
.append(".class, ")
|
||||
.append(paramName)
|
||||
.append(");");
|
||||
if (convertToDataExceptions) {
|
||||
if (dataRepository) {
|
||||
declaration
|
||||
.append("\n");
|
||||
}
|
||||
|
|
|
@ -37,6 +37,11 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
|
|||
return paramNullability.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean singleResult() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAttributeDeclarationString() {
|
||||
final StringBuilder declaration = new StringBuilder();
|
||||
|
|
|
@ -39,12 +39,14 @@ public class QueryMethod extends AbstractQueryMethod {
|
|||
boolean belongsToDao,
|
||||
String sessionType,
|
||||
String sessionName,
|
||||
boolean addNonnullAnnotation) {
|
||||
boolean addNonnullAnnotation,
|
||||
boolean dataRepository) {
|
||||
super( annotationMetaEntity,
|
||||
methodName,
|
||||
paramNames, paramTypes, returnTypeName,
|
||||
sessionType, sessionName,
|
||||
belongsToDao, addNonnullAnnotation );
|
||||
belongsToDao, addNonnullAnnotation,
|
||||
dataRepository );
|
||||
this.queryString = queryString;
|
||||
this.containerTypeName = containerTypeName;
|
||||
this.isUpdate = isUpdate;
|
||||
|
@ -66,6 +68,11 @@ public class QueryMethod extends AbstractQueryMethod {
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean singleResult() {
|
||||
return containerTypeName == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAttributeDeclarationString() {
|
||||
final List<String> paramTypes = parameterTypes();
|
||||
|
@ -79,8 +86,13 @@ public class QueryMethod extends AbstractQueryMethod {
|
|||
.append(methodName);
|
||||
parameters( paramTypes, declaration );
|
||||
declaration
|
||||
.append(" {")
|
||||
.append("\n\t");
|
||||
.append(" {\n");
|
||||
if (dataRepository) {
|
||||
declaration
|
||||
.append("\ttry {\n\t");
|
||||
}
|
||||
declaration
|
||||
.append("\t");
|
||||
if ( returnTypeName == null || !returnTypeName.equals("void") ) {
|
||||
declaration
|
||||
.append("return ");
|
||||
|
@ -107,7 +119,8 @@ public class QueryMethod extends AbstractQueryMethod {
|
|||
declaration.append(")");
|
||||
boolean unwrapped = setParameters( paramTypes, declaration );
|
||||
execute( declaration, unwrapped );
|
||||
declaration.append(";\n}");
|
||||
convertExceptions( declaration );
|
||||
declaration.append("\n}");
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue