HHH-17772 Jakarta exception conversion for @Find methods
This commit is contained in:
parent
394c9ef03f
commit
25d7cc8681
|
@ -20,6 +20,7 @@ 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,
|
||||||
|
@ -31,7 +32,8 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
|
||||||
List<String> fetchProfiles,
|
List<String> fetchProfiles,
|
||||||
List<String> paramNames,
|
List<String> paramNames,
|
||||||
List<String> paramTypes,
|
List<String> paramTypes,
|
||||||
boolean addNonnullAnnotation) {
|
boolean addNonnullAnnotation,
|
||||||
|
boolean convertToDataExceptions) {
|
||||||
super( annotationMetaEntity,
|
super( annotationMetaEntity,
|
||||||
methodName,
|
methodName,
|
||||||
paramNames, paramTypes, entity,
|
paramNames, paramTypes, entity,
|
||||||
|
@ -39,6 +41,7 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
|
||||||
belongsToDao, addNonnullAnnotation );
|
belongsToDao, addNonnullAnnotation );
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
this.fetchProfiles = fetchProfiles;
|
this.fetchProfiles = fetchProfiles;
|
||||||
|
this.convertToDataExceptions = convertToDataExceptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -172,13 +175,39 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
|
||||||
declaration
|
declaration
|
||||||
.append(" ")
|
.append(" ")
|
||||||
.append(methodName);
|
.append(methodName);
|
||||||
parameters( paramTypes, declaration) ;
|
parameters( paramTypes, declaration ) ;
|
||||||
declaration
|
declaration
|
||||||
.append(" {")
|
.append(" {\n");
|
||||||
.append("\n\treturn ")
|
if (convertToDataExceptions) {
|
||||||
|
declaration
|
||||||
|
.append("\ttry {\n\t");
|
||||||
|
}
|
||||||
|
declaration
|
||||||
|
.append("\treturn ")
|
||||||
.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
|
||||||
|
|
|
@ -111,6 +111,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
||||||
private final Map<String, MetaAttribute> members;
|
private final Map<String, MetaAttribute> members;
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final boolean managed;
|
private final boolean managed;
|
||||||
|
private boolean dataRepository;
|
||||||
|
|
||||||
private AccessTypeInformation entityAccessTypeInfo;
|
private AccessTypeInformation entityAccessTypeInfo;
|
||||||
|
|
||||||
|
@ -312,8 +313,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dataRepository = hasAnnotation( element, JD_REPOSITORY );
|
||||||
findSessionGetter( element );
|
findSessionGetter( element );
|
||||||
if ( !dao && hasAnnotation( element, JD_REPOSITORY ) ) {
|
if ( !dao && dataRepository) {
|
||||||
dao = true;
|
dao = true;
|
||||||
sessionType = HIB_STATELESS_SESSION;
|
sessionType = HIB_STATELESS_SESSION;
|
||||||
addDaoConstructor( null );
|
addDaoConstructor( null );
|
||||||
|
@ -753,7 +755,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
||||||
sessionType[1],
|
sessionType[1],
|
||||||
enabledFetchProfiles( method ),
|
enabledFetchProfiles( method ),
|
||||||
orderByList( method, entity ),
|
orderByList( method, entity ),
|
||||||
context.addNonnullAnnotation()
|
context.addNonnullAnnotation(),
|
||||||
|
dataRepository
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -906,7 +909,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
||||||
sessionType[0],
|
sessionType[0],
|
||||||
sessionType[1],
|
sessionType[1],
|
||||||
enabledFetchProfiles( method ),
|
enabledFetchProfiles( method ),
|
||||||
context.addNonnullAnnotation()
|
context.addNonnullAnnotation(),
|
||||||
|
dataRepository
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -925,7 +929,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
||||||
sessionType[1],
|
sessionType[1],
|
||||||
enabledFetchProfiles( method ),
|
enabledFetchProfiles( method ),
|
||||||
orderByList( method, entity ),
|
orderByList( method, entity ),
|
||||||
context.addNonnullAnnotation()
|
context.addNonnullAnnotation(),
|
||||||
|
dataRepository
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -957,7 +962,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
||||||
sessionType[0],
|
sessionType[0],
|
||||||
sessionType[1],
|
sessionType[1],
|
||||||
profiles,
|
profiles,
|
||||||
context.addNonnullAnnotation()
|
context.addNonnullAnnotation(),
|
||||||
|
dataRepository
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
@ -974,7 +980,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
||||||
sessionType[0],
|
sessionType[0],
|
||||||
sessionType[1],
|
sessionType[1],
|
||||||
profiles,
|
profiles,
|
||||||
context.addNonnullAnnotation()
|
context.addNonnullAnnotation(),
|
||||||
|
dataRepository
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
@ -993,7 +1000,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
|
||||||
sessionType[1],
|
sessionType[1],
|
||||||
profiles,
|
profiles,
|
||||||
orderByList( method, entity ),
|
orderByList( method, entity ),
|
||||||
context.addNonnullAnnotation()
|
context.addNonnullAnnotation(),
|
||||||
|
dataRepository
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -35,9 +35,11 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
||||||
String sessionName,
|
String sessionName,
|
||||||
List<String> fetchProfiles,
|
List<String> fetchProfiles,
|
||||||
List<OrderBy> orderBys,
|
List<OrderBy> orderBys,
|
||||||
boolean addNonnullAnnotation) {
|
boolean addNonnullAnnotation,
|
||||||
|
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 );
|
||||||
this.containerType = containerType;
|
this.containerType = containerType;
|
||||||
this.paramNullability = paramNullability;
|
this.paramNullability = paramNullability;
|
||||||
this.orderBys = orderBys;
|
this.orderBys = orderBys;
|
||||||
|
@ -62,13 +64,25 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
||||||
declaration
|
declaration
|
||||||
.append(" {");
|
.append(" {");
|
||||||
nullChecks(paramTypes, declaration);
|
nullChecks(paramTypes, declaration);
|
||||||
|
|
||||||
createQuery(declaration);
|
createQuery(declaration);
|
||||||
where(declaration, paramTypes);
|
where(declaration, paramTypes);
|
||||||
orderBy(paramTypes, declaration);
|
orderBy(paramTypes, declaration);
|
||||||
|
executeQuery(declaration, paramTypes);
|
||||||
|
convertExceptions( declaration );
|
||||||
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(sessionName)
|
||||||
.append(".createQuery(query)");
|
.append(".createQuery(query)");
|
||||||
final boolean hasOrderParameter =
|
final boolean hasOrderParameter =
|
||||||
|
@ -89,13 +103,13 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
||||||
final String paramName = paramNames.get(i);
|
final String paramName = paramNames.get(i);
|
||||||
final String paramType = paramTypes.get(i);
|
final String paramType = paramTypes.get(i);
|
||||||
if ( isPageParam(paramType) ) {
|
if ( isPageParam(paramType) ) {
|
||||||
setPage( declaration, paramName, paramType );
|
setPage(declaration, paramName, paramType );
|
||||||
}
|
}
|
||||||
else if ( isOrderParam(paramType) && !isJakartaSortParam(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 ( containerType == null) {
|
||||||
if ( unwrap || hasEnabledFetchProfiles ) {
|
if ( unwrap || hasEnabledFetchProfiles ) {
|
||||||
declaration.append("\n\t\t\t");
|
declaration.append("\n\t\t\t");
|
||||||
|
@ -111,8 +125,11 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
||||||
.append(".getResultList()");
|
.append(".getResultList()");
|
||||||
}
|
}
|
||||||
declaration
|
declaration
|
||||||
.append(";\n}");
|
.append(';');
|
||||||
return declaration.toString();
|
if (convertToDataExceptions) {
|
||||||
|
declaration
|
||||||
|
.append('\n');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createQuery(StringBuilder declaration) {
|
private void createQuery(StringBuilder declaration) {
|
||||||
|
|
|
@ -23,9 +23,10 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
||||||
String sessionType,
|
String sessionType,
|
||||||
String sessionName,
|
String sessionName,
|
||||||
List<String> fetchProfiles,
|
List<String> fetchProfiles,
|
||||||
boolean addNonnullAnnotation) {
|
boolean addNonnullAnnotation,
|
||||||
|
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 );
|
||||||
this.paramName = idParameterName( paramNames, paramTypes );
|
this.paramName = idParameterName( paramNames, paramTypes );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,6 +55,8 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
||||||
else {
|
else {
|
||||||
findWithFetchProfiles( declaration );
|
findWithFetchProfiles( declaration );
|
||||||
}
|
}
|
||||||
|
convertExceptions( declaration );
|
||||||
|
declaration.append("\n}");
|
||||||
return declaration.toString();
|
return declaration.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +70,11 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
||||||
declaration
|
declaration
|
||||||
.append("\n\t\t\t.load(")
|
.append("\n\t\t\t.load(")
|
||||||
.append(paramName)
|
.append(paramName)
|
||||||
.append(");\n}");
|
.append(");");
|
||||||
|
if (convertToDataExceptions) {
|
||||||
|
declaration
|
||||||
|
.append("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void findWithNoFetchProfiles(StringBuilder declaration) {
|
private void findWithNoFetchProfiles(StringBuilder declaration) {
|
||||||
|
@ -76,7 +83,10 @@ public class IdFinderMethod extends AbstractFinderMethod {
|
||||||
.append(annotationMetaEntity.importType(entity))
|
.append(annotationMetaEntity.importType(entity))
|
||||||
.append(".class, ")
|
.append(".class, ")
|
||||||
.append(paramName)
|
.append(paramName)
|
||||||
.append(");")
|
.append(");");
|
||||||
.append("\n}");
|
if (convertToDataExceptions) {
|
||||||
|
declaration
|
||||||
|
.append("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,9 +24,10 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
|
||||||
String sessionType,
|
String sessionType,
|
||||||
String sessionName,
|
String sessionName,
|
||||||
List<String> fetchProfiles,
|
List<String> fetchProfiles,
|
||||||
boolean addNonnullAnnotation) {
|
boolean addNonnullAnnotation,
|
||||||
|
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 );
|
||||||
this.paramNullability = paramNullability;
|
this.paramNullability = paramNullability;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +49,8 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
|
||||||
else {
|
else {
|
||||||
findBlockingly( declaration );
|
findBlockingly( declaration );
|
||||||
}
|
}
|
||||||
declaration.append(";\n}");
|
convertExceptions( declaration );
|
||||||
|
declaration.append("\n}");
|
||||||
return declaration.toString();
|
return declaration.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +74,7 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
declaration
|
declaration
|
||||||
.append("\n\t\t\t.load()");
|
.append("\n\t\t\t.load();");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void findReactively(StringBuilder declaration) {
|
private void findReactively(StringBuilder declaration) {
|
||||||
|
@ -121,7 +123,7 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
|
||||||
if (composite) {
|
if (composite) {
|
||||||
declaration.append("\n\t\t\t)\n\t");
|
declaration.append("\n\t\t\t)\n\t");
|
||||||
}
|
}
|
||||||
declaration.append(")");
|
declaration.append(");");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,9 @@ public interface BookAuthorRepository {
|
||||||
@Find
|
@Find
|
||||||
Book book(String isbn);
|
Book book(String isbn);
|
||||||
|
|
||||||
|
@Find
|
||||||
|
Book byTitleAndDate(String title, LocalDate publicationDate);
|
||||||
|
|
||||||
@Find
|
@Find
|
||||||
Book bookById(@By("isbn") String id);
|
Book bookById(@By("isbn") String id);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue