HHH-17772 Jakarta exception conversion for @Find methods

This commit is contained in:
Gavin King 2024-02-23 23:10:44 +01:00
parent 394c9ef03f
commit 25d7cc8681
6 changed files with 100 additions and 31 deletions

View File

@ -20,6 +20,7 @@ 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,
@ -31,7 +32,8 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
List<String> fetchProfiles,
List<String> paramNames,
List<String> paramTypes,
boolean addNonnullAnnotation) {
boolean addNonnullAnnotation,
boolean convertToDataExceptions) {
super( annotationMetaEntity,
methodName,
paramNames, paramTypes, entity,
@ -39,6 +41,7 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
belongsToDao, addNonnullAnnotation );
this.entity = entity;
this.fetchProfiles = fetchProfiles;
this.convertToDataExceptions = convertToDataExceptions;
}
@Override
@ -172,13 +175,39 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
declaration
.append(" ")
.append(methodName);
parameters( paramTypes, declaration) ;
parameters( paramTypes, declaration ) ;
declaration
.append(" {")
.append("\n\treturn ")
.append(" {\n");
if (convertToDataExceptions) {
declaration
.append("\ttry {\n\t");
}
declaration
.append("\treturn ")
.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

View File

@ -111,6 +111,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private final Map<String, MetaAttribute> members;
private final Context context;
private final boolean managed;
private boolean dataRepository;
private AccessTypeInformation entityAccessTypeInfo;
@ -312,8 +313,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
}
dataRepository = hasAnnotation( element, JD_REPOSITORY );
findSessionGetter( element );
if ( !dao && hasAnnotation( element, JD_REPOSITORY ) ) {
if ( !dao && dataRepository) {
dao = true;
sessionType = HIB_STATELESS_SESSION;
addDaoConstructor( null );
@ -753,7 +755,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
sessionType[1],
enabledFetchProfiles( method ),
orderByList( method, entity ),
context.addNonnullAnnotation()
context.addNonnullAnnotation(),
dataRepository
)
);
}
@ -906,7 +909,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
sessionType[0],
sessionType[1],
enabledFetchProfiles( method ),
context.addNonnullAnnotation()
context.addNonnullAnnotation(),
dataRepository
)
);
}
@ -925,7 +929,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
sessionType[1],
enabledFetchProfiles( method ),
orderByList( method, entity ),
context.addNonnullAnnotation()
context.addNonnullAnnotation(),
dataRepository
)
);
}
@ -957,7 +962,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
sessionType[0],
sessionType[1],
profiles,
context.addNonnullAnnotation()
context.addNonnullAnnotation(),
dataRepository
)
);
break;
@ -974,7 +980,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
sessionType[0],
sessionType[1],
profiles,
context.addNonnullAnnotation()
context.addNonnullAnnotation(),
dataRepository
)
);
break;
@ -993,7 +1000,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
sessionType[1],
profiles,
orderByList( method, entity ),
context.addNonnullAnnotation()
context.addNonnullAnnotation(),
dataRepository
)
);
break;

View File

@ -35,9 +35,11 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
String sessionName,
List<String> fetchProfiles,
List<OrderBy> orderBys,
boolean addNonnullAnnotation) {
boolean addNonnullAnnotation,
boolean dataRepository) {
super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, addNonnullAnnotation );
paramNames, paramTypes, addNonnullAnnotation,
dataRepository && containerType == null );
this.containerType = containerType;
this.paramNullability = paramNullability;
this.orderBys = orderBys;
@ -62,13 +64,25 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
declaration
.append(" {");
nullChecks(paramTypes, declaration);
createQuery(declaration);
where(declaration, paramTypes);
orderBy(paramTypes, declaration);
executeQuery(declaration, paramTypes);
convertExceptions( declaration );
declaration
.append("\n\treturn ")
.append("\n}");
return declaration.toString();
}
private void executeQuery(StringBuilder declaration, List<String> paramTypes) {
declaration
.append('\n');
if (convertToDataExceptions) {
declaration
.append("\ttry {\n\t");
}
declaration
.append("\treturn ")
.append(sessionName)
.append(".createQuery(query)");
final boolean hasOrderParameter =
@ -89,13 +103,13 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
final String paramName = paramNames.get(i);
final String paramType = paramTypes.get(i);
if ( isPageParam(paramType) ) {
setPage( declaration, paramName, paramType );
setPage(declaration, paramName, paramType );
}
else if ( isOrderParam(paramType) && !isJakartaSortParam(paramType) ) {
setOrder( declaration, true, paramName, paramType );
setOrder(declaration, true, paramName, paramType );
}
}
enableFetchProfile( declaration );
enableFetchProfile(declaration);
if ( containerType == null) {
if ( unwrap || hasEnabledFetchProfiles ) {
declaration.append("\n\t\t\t");
@ -111,8 +125,11 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
.append(".getResultList()");
}
declaration
.append(";\n}");
return declaration.toString();
.append(';');
if (convertToDataExceptions) {
declaration
.append('\n');
}
}
private void createQuery(StringBuilder declaration) {

View File

@ -23,9 +23,10 @@ public class IdFinderMethod extends AbstractFinderMethod {
String sessionType,
String sessionName,
List<String> fetchProfiles,
boolean addNonnullAnnotation) {
boolean addNonnullAnnotation,
boolean dataRepository) {
super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, addNonnullAnnotation );
paramNames, paramTypes, addNonnullAnnotation, dataRepository );
this.paramName = idParameterName( paramNames, paramTypes );
}
@ -54,6 +55,8 @@ public class IdFinderMethod extends AbstractFinderMethod {
else {
findWithFetchProfiles( declaration );
}
convertExceptions( declaration );
declaration.append("\n}");
return declaration.toString();
}
@ -67,7 +70,11 @@ public class IdFinderMethod extends AbstractFinderMethod {
declaration
.append("\n\t\t\t.load(")
.append(paramName)
.append(");\n}");
.append(");");
if (convertToDataExceptions) {
declaration
.append("\n");
}
}
private void findWithNoFetchProfiles(StringBuilder declaration) {
@ -76,7 +83,10 @@ public class IdFinderMethod extends AbstractFinderMethod {
.append(annotationMetaEntity.importType(entity))
.append(".class, ")
.append(paramName)
.append(");")
.append("\n}");
.append(");");
if (convertToDataExceptions) {
declaration
.append("\n");
}
}
}

View File

@ -24,9 +24,10 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
String sessionType,
String sessionName,
List<String> fetchProfiles,
boolean addNonnullAnnotation) {
boolean addNonnullAnnotation,
boolean dataRepository) {
super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, addNonnullAnnotation );
paramNames, paramTypes, addNonnullAnnotation, dataRepository );
this.paramNullability = paramNullability;
}
@ -48,7 +49,8 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
else {
findBlockingly( declaration );
}
declaration.append(";\n}");
convertExceptions( declaration );
declaration.append("\n}");
return declaration.toString();
}
@ -72,7 +74,7 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
}
}
declaration
.append("\n\t\t\t.load()");
.append("\n\t\t\t.load();");
}
private void findReactively(StringBuilder declaration) {
@ -121,7 +123,7 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
if (composite) {
declaration.append("\n\t\t\t)\n\t");
}
declaration.append(")");
declaration.append(");");
}
}

View File

@ -26,6 +26,9 @@ public interface BookAuthorRepository {
@Find
Book book(String isbn);
@Find
Book byTitleAndDate(String title, LocalDate publicationDate);
@Find
Book bookById(@By("isbn") String id);