HHH-17772 support for Limit and Sort

This commit is contained in:
Gavin King 2024-02-23 15:16:54 +01:00
parent 4cfc3fb97d
commit bd63c4af50
6 changed files with 62 additions and 11 deletions

View File

@ -8,10 +8,13 @@ package org.hibernate.jpamodelgen.annotation;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.jpamodelgen.util.Constants;
import org.hibernate.query.Order;
import org.hibernate.query.SortDirection;
import java.util.List;
import java.util.Locale;
import static org.hibernate.jpamodelgen.util.Constants.JD_SORT;
import static org.hibernate.jpamodelgen.util.StringUtil.getUpperUnderscoreCaseFromLowerCamelCase;
/**
@ -197,4 +200,29 @@ public abstract class AbstractFinderMethod extends AbstractQueryMethod {
declaration
.append(belongsToDao ? "@Override\npublic " : "public static ");
}
@Override
boolean setOrder(StringBuilder declaration, boolean unwrapped, String paramName, String paramType) {
final boolean jakartaSort = paramType.startsWith(JD_SORT);
if ( jakartaSort ) {
annotationMetaEntity.staticImport(SortDirection.class.getName(), "ASCENDING");
annotationMetaEntity.staticImport(SortDirection.class.getName(), "DESCENDING");
declaration
.append("\n\t\t\t.setOrder(")
.append(annotationMetaEntity.importType(Order.class.getName()))
.append(".by(")
.append(annotationMetaEntity.importType(entity))
.append(".class, ")
.append(paramName)
.append(".property()")
.append(",\n\t\t\t\t\t")
.append(paramName)
.append(".isAscending() ? ASCENDING : DESCENDING")
.append("))");
return true;
}
else {
return super.setOrder(declaration, unwrapped, paramName, paramType);
}
}
}

View File

@ -16,6 +16,9 @@ import org.hibernate.query.Page;
import java.util.List;
import static java.util.stream.Collectors.toList;
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_SORT;
import static org.hibernate.jpamodelgen.util.Constants.SESSION_TYPES;
import static org.hibernate.jpamodelgen.util.TypeUtils.isPrimitive;
@ -171,15 +174,24 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
return Constants.MUTINY_SESSION.equals(sessionType);
}
void setPage(StringBuilder declaration, String paramName) {
if ( isUsingEntityManager() ) {
void setPage(StringBuilder declaration, String paramName, String paramType) {
boolean jakartaLimit = JD_LIMIT.equals(paramType);
if ( jakartaLimit || isUsingEntityManager() ) {
declaration
.append("\n\t\t\t.setFirstResult(");
if (jakartaLimit) {
declaration.append("(int) ");
}
declaration
.append("\n\t\t\t.setFirstResult(")
.append(paramName)
.append(".getFirstResult())")
.append('.')
.append(jakartaLimit ? "startAt" : "getFirstResult")
.append("())")
.append("\n\t\t\t.setMaxResults(")
.append(paramName)
.append(".getMaxResults())");
.append('.')
.append(jakartaLimit ? "maxResults" : "getMaxResults")
.append("())");
}
else {
declaration
@ -218,11 +230,14 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
}
static boolean isPageParam(String parameterType) {
return Page.class.getName().equals(parameterType);
return Page.class.getName().equals(parameterType)
|| JD_LIMIT.equals(parameterType);
}
static boolean isOrderParam(String parameterType) {
return parameterType.startsWith(Order.class.getName())
|| parameterType.startsWith(List.class.getName() + "<" + Order.class.getName());
|| parameterType.startsWith(List.class.getName() + "<" + Order.class.getName())
|| parameterType.startsWith(JD_SORT)
|| parameterType.startsWith(JD_ORDER);
}
}

View File

@ -73,8 +73,10 @@ import static org.hibernate.jpamodelgen.util.Constants.HQL;
import static org.hibernate.jpamodelgen.util.Constants.JD_DELETE;
import static org.hibernate.jpamodelgen.util.Constants.JD_FIND;
import static org.hibernate.jpamodelgen.util.Constants.JD_INSERT;
import static org.hibernate.jpamodelgen.util.Constants.JD_ORDER;
import static org.hibernate.jpamodelgen.util.Constants.JD_QUERY;
import static org.hibernate.jpamodelgen.util.Constants.JD_REPOSITORY;
import static org.hibernate.jpamodelgen.util.Constants.JD_SORT;
import static org.hibernate.jpamodelgen.util.Constants.JD_UPDATE;
import static org.hibernate.jpamodelgen.util.Constants.MUTINY_SESSION;
import static org.hibernate.jpamodelgen.util.Constants.SESSION_TYPES;
@ -783,12 +785,15 @@ public class AnnotationMetaEntity extends AnnotationMeta {
return getTypeArgument( arrayType.getComponentType() );
case DECLARED:
final DeclaredType type = (DeclaredType) parameterType;
if ( parameterType.toString().startsWith( List.class.getName() ) ) {
final String parameterTypeName = parameterType.toString();
if ( parameterTypeName.startsWith( List.class.getName() )
|| parameterTypeName.startsWith( JD_ORDER ) ) {
for (TypeMirror arg : type.getTypeArguments()) {
return getTypeArgument( arg );
}
}
else if ( parameterType.toString().startsWith( Order.class.getName() ) ) {
else if ( parameterTypeName.startsWith( Order.class.getName() )
|| parameterTypeName.startsWith( JD_SORT ) ) {
for ( TypeMirror arg : type.getTypeArguments() ) {
switch ( arg.getKind() ) {
case WILDCARD:

View File

@ -156,7 +156,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
final String paramName = paramNames.get(i);
final String paramType = paramTypes.get(i);
if ( isPageParam(paramType) ) {
setPage( declaration, paramName );
setPage( declaration, paramName, paramType );
}
else if ( isOrderParam(paramType) ) {
setOrder( declaration, true, paramName, paramType );

View File

@ -156,7 +156,7 @@ public class QueryMethod extends AbstractQueryMethod {
setOrdinalParameter( declaration, ordinal, paramName );
}
else if ( isPageParam(paramType) ) {
setPage( declaration, paramName );
setPage( declaration, paramName, paramType );
}
else if ( isOrderParam(paramType) ) {
unwrapped = setOrder( declaration, unwrapped, paramName, paramType );

View File

@ -67,6 +67,9 @@ public final class Constants {
public static final String JD_INSERT = "jakarta.data.repository.Insert";
public static final String JD_UPDATE = "jakarta.data.repository.Update";
public static final String JD_DELETE = "jakarta.data.repository.Delete";
public static final String JD_LIMIT = "jakarta.data.Limit";
public static final String JD_SORT = "jakarta.data.Sort";
public static final String JD_ORDER = "jakarta.data.Order";
public static final String CHECK_HQL = "org.hibernate.annotations.processing.CheckHQL";