HHH-17772 cleanup after sorting rework

This commit is contained in:
Gavin King 2024-02-29 20:19:41 +01:00
parent 7f4f760ac8
commit 5d2527383e
6 changed files with 48 additions and 101 deletions

View File

@ -22,7 +22,7 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
final String entity;
final List<String> fetchProfiles;
public AbstractFinderMethod(
AbstractFinderMethod(
AnnotationMetaEntity annotationMetaEntity,
String methodName,
String entity,
@ -32,13 +32,15 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
List<String> fetchProfiles,
List<String> paramNames,
List<String> paramTypes,
List<OrderBy> orderBys,
boolean addNonnullAnnotation,
boolean convertToDataExceptions) {
super( annotationMetaEntity,
methodName,
paramNames, paramTypes, entity,
sessionType, sessionName,
belongsToDao, addNonnullAnnotation,
belongsToDao, orderBys,
addNonnullAnnotation,
convertToDataExceptions );
this.entity = entity;
this.fetchProfiles = fetchProfiles;
@ -204,9 +206,4 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
declaration
.append(belongsToDao ? "@Override\npublic " : "public static ");
}
@Override
String getSortableEntityClass() {
return entity;
}
}

View File

@ -10,30 +10,12 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.AssertionFailure;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.Metamodel;
import org.hibernate.jpamodelgen.util.Constants;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.hibernate.jpamodelgen.util.Constants.HIB_KEYED_PAGE;
import static org.hibernate.jpamodelgen.util.Constants.HIB_KEYED_RESULT_LIST;
import static org.hibernate.jpamodelgen.util.Constants.HIB_ORDER;
import static org.hibernate.jpamodelgen.util.Constants.HIB_PAGE;
import static org.hibernate.jpamodelgen.util.Constants.HIB_SELECTION_QUERY;
import static org.hibernate.jpamodelgen.util.Constants.JD_KEYED_PAGE;
import static org.hibernate.jpamodelgen.util.Constants.JD_KEYED_SLICE;
import static org.hibernate.jpamodelgen.util.Constants.JD_LIMIT;
import static org.hibernate.jpamodelgen.util.Constants.JD_ORDER;
import static org.hibernate.jpamodelgen.util.Constants.JD_PAGE;
import static org.hibernate.jpamodelgen.util.Constants.JD_PAGE_REQUEST;
import static org.hibernate.jpamodelgen.util.Constants.JD_SLICE;
import static org.hibernate.jpamodelgen.util.Constants.JD_SORT;
import static org.hibernate.jpamodelgen.util.Constants.LIST;
import static org.hibernate.jpamodelgen.util.Constants.OPTIONAL;
import static org.hibernate.jpamodelgen.util.Constants.SESSION_TYPES;
import static org.hibernate.jpamodelgen.util.Constants.STREAM;
import static org.hibernate.jpamodelgen.util.Constants.*;
import static org.hibernate.jpamodelgen.util.TypeUtils.isPrimitive;
/**
@ -48,10 +30,11 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
final String sessionType;
final String sessionName;
final boolean belongsToDao;
final List<OrderBy> orderBys;
final boolean addNonnullAnnotation;
final boolean dataRepository;
public AbstractQueryMethod(
AbstractQueryMethod(
AnnotationMetaEntity annotationMetaEntity,
String methodName,
List<String> paramNames, List<String> paramTypes,
@ -59,6 +42,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
String sessionType,
String sessionName,
boolean belongsToDao,
List<OrderBy> orderBys,
boolean addNonnullAnnotation,
boolean dataRepository) {
this.annotationMetaEntity = annotationMetaEntity;
@ -69,6 +53,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
this.sessionType = sessionType;
this.sessionName = sessionName;
this.belongsToDao = belongsToDao;
this.orderBys = orderBys;
this.addNonnullAnnotation = addNonnullAnnotation;
this.dataRepository = dataRepository;
}
@ -112,6 +97,16 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
return type.endsWith("...") ? stripped + "..." : stripped;
}
void preamble(StringBuilder declaration, StringBuilder returnType, List<String> paramTypes) {
declaration
.append(returnType)
.append(" ")
.append(methodName);
parameters( paramTypes, declaration );
declaration
.append(" {\n");
}
void parameters(List<String> paramTypes, StringBuilder declaration) {
declaration
.append("(");
@ -180,15 +175,15 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
}
boolean isUsingEntityManager() {
return Constants.ENTITY_MANAGER.equals(sessionType);
return ENTITY_MANAGER.equals(sessionType);
}
boolean isUsingStatelessSession() {
return Constants.HIB_STATELESS_SESSION.equals(sessionType);
return HIB_STATELESS_SESSION.equals(sessionType);
}
boolean isReactive() {
return Constants.MUTINY_SESSION.equals(sessionType);
return MUTINY_SESSION.equals(sessionType);
}
void setPage(StringBuilder declaration, String paramName, String paramType) {
@ -290,17 +285,11 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
declaration.append("\n}");
}
@Nullable String getSortableEntityClass() {
return returnTypeName;
}
void unwrapQuery(StringBuilder declaration, boolean unwrapped) {
if ( !unwrapped && isUsingEntityManager() ) {
declaration
.append("\t\t\t.unwrap(");
final String selectionQuery = annotationMetaEntity.importType(HIB_SELECTION_QUERY);
declaration
.append(selectionQuery)
.append("\t\t\t.unwrap(")
.append(annotationMetaEntity.importType(HIB_SELECTION_QUERY))
.append(".class)\n");
}
}
@ -346,8 +335,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
annotationMetaEntity.staticImport("org.hibernate.query.Order", "by");
annotationMetaEntity.staticImport("org.hibernate.query.Page", "page");
annotationMetaEntity.staticImport(Collectors.class.getName(), "toList");
final String entityClass = getSortableEntityClass();
if ( entityClass == null ) {
if ( returnTypeName == null ) {
throw new AssertionFailure("entity class cannot be null");
}
else {
@ -355,7 +343,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
.append(MAKE_KEYED_PAGE
.replace("pageRequest",
parameterName(JD_PAGE_REQUEST, paramTypes, paramNames))
.replace("Entity", annotationMetaEntity.importType(entityClass)));
.replace("Entity", annotationMetaEntity.importType(returnTypeName)));
}
}
@ -394,10 +382,6 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
void setParameters(StringBuilder declaration, List<String> paramTypes) {}
List<OrderBy> getOrderBys() {
return emptyList();
}
void tryReturn(StringBuilder declaration, List<String> paramTypes, @Nullable String containerType) {
if ( isJakartaKeyedSlice(containerType) ) {
makeKeyedPage( declaration, paramTypes );
@ -422,14 +406,13 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
.append('\t');
if ( isJakartaKeyedSlice(containerType)
|| isJakartaSlice(containerType) ) {
final String entityClass = getSortableEntityClass();
if ( entityClass != null && isUsingEntityManager() ) {
if ( returnTypeName != null && isUsingEntityManager() ) {
// this is necessary to avoid losing the type
// after unwrapping the Query object
declaration
.append(annotationMetaEntity.importType(HIB_KEYED_RESULT_LIST))
.append('<')
.append(annotationMetaEntity.importType(entityClass))
.append(annotationMetaEntity.importType(returnTypeName))
.append('>');
}
else {
@ -454,28 +437,27 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
setParameters( declaration, paramTypes );
unwrapQuery( declaration, !isUsingEntityManager() );
declaration
.append("\t\t\t\t\t\t.getResultCount();\n");
.append("\t\t\t.getResultCount();\n");
}
void collectOrdering(StringBuilder declaration, List<String> paramTypes) {
final String entityClass = getSortableEntityClass();
if ( hasOrdering(paramTypes) ) {
if ( entityClass != null ) {
if ( returnTypeName != null ) {
declaration
.append("\tvar _orders = new ")
.append(annotationMetaEntity.importType("java.util.ArrayList"))
.append("<")
.append(annotationMetaEntity.importType(HIB_ORDER))
.append("<? super ")
.append(annotationMetaEntity.importType(entityClass))
.append(annotationMetaEntity.importType(returnTypeName))
.append(">>();\n");
// static orders declared using @OrderBy must come first
for ( OrderBy orderBy : getOrderBys() ) {
for ( OrderBy orderBy : orderBys ) {
declaration
.append("\t_orders.add(")
.append(annotationMetaEntity.staticImport(HIB_ORDER, "by"))
.append('(')
.append(annotationMetaEntity.importType(entityClass))
.append(annotationMetaEntity.importType(returnTypeName))
.append(".class, \"")
.append(orderBy.fieldName)
.append("\", ")
@ -517,7 +499,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
.append("\t\t_orders.add(")
.append(annotationMetaEntity.staticImport(HIB_ORDER, "by"))
.append('(')
.append(annotationMetaEntity.importType(entityClass))
.append(annotationMetaEntity.importType(returnTypeName))
.append(".class, _sort.property(),")
.append("\n\t\t\t\t\t\t")
.append("_sort.isAscending() ? ASCENDING : DESCENDING,")
@ -534,7 +516,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
.append("\t\t_orders.add(")
.append(annotationMetaEntity.staticImport(HIB_ORDER, "by"))
.append('(')
.append(annotationMetaEntity.importType(entityClass))
.append(annotationMetaEntity.importType(returnTypeName))
.append(".class, _sort.property(),")
.append("\n\t\t\t\t\t\t")
.append("_sort.isAscending() ? ASCENDING : DESCENDING,")
@ -547,7 +529,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
.append("\t_orders.add(")
.append(annotationMetaEntity.staticImport(HIB_ORDER, "by"))
.append('(')
.append(annotationMetaEntity.importType(entityClass))
.append(annotationMetaEntity.importType(returnTypeName))
.append(".class, ")
.append(name)
.append(".property(),")
@ -565,7 +547,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
private boolean hasOrdering(List<String> paramTypes) {
return paramTypes.stream().anyMatch(AbstractQueryMethod::isOrderParam)
|| !getOrderBys().isEmpty();
|| !orderBys.isEmpty();
}
boolean unwrapIfNecessary(StringBuilder declaration, @Nullable String containerType, boolean unwrapped) {
@ -628,8 +610,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
break;
case JD_KEYED_SLICE:
case JD_KEYED_PAGE:
final String entityClass = getSortableEntityClass();
if ( entityClass == null ) {
if ( returnTypeName == null ) {
throw new AssertionFailure("entity class cannot be null");
}
else {
@ -640,7 +621,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
String fragment = MAKE_KEYED_SLICE
.replace("pageRequest",
parameterName(JD_PAGE_REQUEST, paramTypes, paramNames))
.replace("Entity", annotationMetaEntity.importType(entityClass))
.replace("Entity", annotationMetaEntity.importType(returnTypeName))
.replace("KeysetAwareSliceRecord", implType(containerType));
if ( JD_KEYED_SLICE.equals(containerType) ) {
fragment = fragment.replace("_totalResults, ", "");

View File

@ -22,7 +22,6 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
private final @Nullable String containerType;
private final List<Boolean> paramNullability;
private final List<OrderBy> orderBys;
CriteriaFinderMethod(
AnnotationMetaEntity annotationMetaEntity,
@ -38,11 +37,9 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
boolean addNonnullAnnotation,
boolean dataRepository) {
super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, addNonnullAnnotation,
dataRepository );
paramNames, paramTypes, orderBys, addNonnullAnnotation, dataRepository );
this.containerType = containerType;
this.paramNullability = paramNullability;
this.orderBys = orderBys;
}
@Override
@ -55,18 +52,13 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
return containerType == null;
}
@Override
List<OrderBy> getOrderBys() {
return orderBys;
}
@Override
public String getAttributeDeclarationString() {
final List<String> paramTypes = parameterTypes();
final StringBuilder declaration = new StringBuilder();
comment( declaration );
modifiers( declaration );
preamble( declaration, paramTypes );
preamble( declaration, returnType(), paramTypes );
nullChecks( paramTypes, declaration );
createCriteriaQuery( declaration );
where( declaration, paramTypes );
@ -77,16 +69,6 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
return declaration.toString();
}
private void preamble(StringBuilder declaration, List<String> paramTypes) {
declaration
.append(returnType())
.append(" ")
.append(methodName);
parameters(paramTypes, declaration);
declaration
.append(" {\n");
}
private void executeQuery(StringBuilder declaration, List<String> paramTypes) {
declaration
.append('\n');

View File

@ -8,6 +8,7 @@ package org.hibernate.jpamodelgen.annotation;
import java.util.List;
import static java.util.Collections.emptyList;
import static org.hibernate.jpamodelgen.util.TypeUtils.isPrimitive;
/**
@ -29,7 +30,7 @@ public class IdFinderMethod extends AbstractFinderMethod {
boolean addNonnullAnnotation,
boolean dataRepository) {
super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, addNonnullAnnotation, dataRepository );
paramNames, paramTypes, emptyList(), addNonnullAnnotation, dataRepository );
int idParameter = idParameter(paramNames, paramTypes);
this.paramName = paramNames.get(idParameter);
this.paramType = paramTypes.get(idParameter);

View File

@ -8,6 +8,8 @@ package org.hibernate.jpamodelgen.annotation;
import java.util.List;
import static java.util.Collections.emptyList;
/**
* @author Gavin King
*/
@ -27,7 +29,7 @@ public class NaturalIdFinderMethod extends AbstractFinderMethod {
boolean addNonnullAnnotation,
boolean dataRepository) {
super( annotationMetaEntity, methodName, entity, belongsToDao, sessionType, sessionName, fetchProfiles,
paramNames, paramTypes, addNonnullAnnotation, dataRepository );
paramNames, paramTypes, emptyList(), addNonnullAnnotation, dataRepository );
this.paramNullability = paramNullability;
}

View File

@ -24,7 +24,6 @@ public class QueryMethod extends AbstractQueryMethod {
private final @Nullable String containerType;
private final boolean isUpdate;
private final boolean isNative;
private final List<OrderBy> orderBys;
QueryMethod(
AnnotationMetaEntity annotationMetaEntity,
@ -48,13 +47,13 @@ public class QueryMethod extends AbstractQueryMethod {
methodName,
paramNames, paramTypes, returnTypeName,
sessionType, sessionName,
belongsToDao, addNonnullAnnotation,
belongsToDao, orderBys,
addNonnullAnnotation,
dataRepository );
this.queryString = queryString;
this.containerType = containerType;
this.isUpdate = isUpdate;
this.isNative = isNative;
this.orderBys = orderBys;
}
@Override
@ -77,11 +76,6 @@ public class QueryMethod extends AbstractQueryMethod {
return containerType == null;
}
@Override
List<OrderBy> getOrderBys() {
return orderBys;
}
@Override
public String getAttributeDeclarationString() {
final List<String> paramTypes = parameterTypes();
@ -137,16 +131,6 @@ public class QueryMethod extends AbstractQueryMethod {
}
}
private void preamble(StringBuilder declaration, StringBuilder returnType, List<String> paramTypes) {
declaration
.append(returnType)
.append(" ")
.append(methodName);
parameters( paramTypes, declaration );
declaration
.append(" {\n");
}
private void execute(StringBuilder declaration, boolean unwrapped) {
if ( isUpdate ) {
declaration