HHH-16633 no need to cast to SelectionQuery to call setPage()

This commit is contained in:
Gavin King 2023-07-12 11:26:22 +02:00
parent 0c1a49604e
commit 96e6476199
2 changed files with 78 additions and 48 deletions

View File

@ -548,7 +548,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
else { else {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final List<AnnotationValue> annotationValues = (List<AnnotationValue>) enabledFetchProfiles; final List<AnnotationValue> annotationValues = (List<AnnotationValue>) enabledFetchProfiles;
List<String> result = annotationValues.stream().map(AnnotationValue::toString).collect(toList()); final List<String> result = annotationValues.stream().map(AnnotationValue::toString).collect(toList());
if ( result.stream().anyMatch("<error>"::equals) ) { if ( result.stream().anyMatch("<error>"::equals) ) {
throw new ProcessLaterException(); throw new ProcessLaterException();
} }

View File

@ -109,53 +109,7 @@ public class QueryMethod implements MetaAttribute {
.append(".class"); .append(".class");
} }
declaration.append(")"); declaration.append(")");
boolean unwrapped = !usingEntityManager; boolean unwrapped = setParameters( paramTypes, declaration );
for (int i = 1; i <= paramNames.size(); i++) {
final String paramName = paramNames.get(i-1);
final String paramType = paramTypes.get(i-1);
if ( queryString.contains(":" + paramName) ) {
declaration
.append("\n\t\t\t.setParameter(\"")
.append(paramName)
.append("\", ")
.append(paramName)
.append(")");
}
else if ( queryString.contains("?" + i) ) {
declaration
.append("\n\t\t\t.setParameter(")
.append(i)
.append(", ")
.append(paramName)
.append(")");
}
else if ( isPageParam(paramType) ) {
unwrap( declaration, unwrapped );
unwrapped = true;
declaration
.append("\n\t\t\t.setPage(")
.append(paramName)
.append(")");
}
else if ( isOrderParam(paramType) ) {
unwrap( declaration, unwrapped );
unwrapped = true;
if ( paramType.endsWith("...") ) {
declaration
.append("\n\t\t\t.setOrder(")
.append(annotationMetaEntity.importType(Constants.LIST))
.append(".of(")
.append(paramName)
.append("))");
}
else {
declaration
.append("\n\t\t\t.setOrder(")
.append(paramName)
.append(")");
}
}
}
if ( containerTypeName == null) { if ( containerTypeName == null) {
declaration declaration
.append("\n\t\t\t.getSingleResult()"); .append("\n\t\t\t.getSingleResult()");
@ -179,6 +133,82 @@ public class QueryMethod implements MetaAttribute {
return declaration.toString(); return declaration.toString();
} }
private boolean setParameters(List<String> paramTypes, StringBuilder declaration) {
boolean unwrapped = !usingEntityManager;
for (int i = 1; i <= paramNames.size(); i++) {
final String paramName = paramNames.get(i-1);
final String paramType = paramTypes.get(i-1);
if ( queryString.contains(":" + paramName) ) {
setNamedParameter( declaration, paramName );
}
else if ( queryString.contains("?" + i) ) {
setOrdinalParameter( declaration, i, paramName );
}
else if ( isPageParam(paramType) ) {
setPage( declaration, paramName );
}
else if ( isOrderParam(paramType) ) {
unwrapped = setOrder( declaration, unwrapped, paramName, paramType );
}
}
return unwrapped;
}
private static void setOrdinalParameter(StringBuilder declaration, int i, String paramName) {
declaration
.append("\n\t\t\t.setParameter(")
.append(i)
.append(", ")
.append(paramName)
.append(")");
}
private static void setNamedParameter(StringBuilder declaration, String paramName) {
declaration
.append("\n\t\t\t.setParameter(\"")
.append(paramName)
.append("\", ")
.append(paramName)
.append(")");
}
private void setPage(StringBuilder declaration, String paramName) {
if ( usingEntityManager ) {
declaration
.append("\n\t\t\t.setFirstResult(")
.append(paramName)
.append(".getFirstResult())")
.append("\n\t\t\t.setMaxResults(")
.append(paramName)
.append(".getMaxResults())");
}
else {
declaration
.append("\n\t\t\t.setPage(")
.append(paramName)
.append(")");
}
}
private boolean setOrder(StringBuilder declaration, boolean unwrapped, String paramName, String paramType) {
unwrap( declaration, unwrapped );
if ( paramType.endsWith("...") ) {
declaration
.append("\n\t\t\t.setOrder(")
.append(annotationMetaEntity.importType(Constants.LIST))
.append(".of(")
.append(paramName)
.append("))");
}
else {
declaration
.append("\n\t\t\t.setOrder(")
.append(paramName)
.append(")");
}
return true;
}
private void parameters(List<String> paramTypes, StringBuilder declaration) { private void parameters(List<String> paramTypes, StringBuilder declaration) {
declaration.append("("); declaration.append("(");
if ( !belongsToDao ) { if ( !belongsToDao ) {