HHH-17772 misc improvements to impl of Sort parameters

This commit is contained in:
Gavin King 2024-02-24 01:21:32 +01:00
parent 2281805e91
commit 862a967a17
2 changed files with 19 additions and 22 deletions

View File

@ -222,14 +222,14 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
.append(annotationMetaEntity.importType(ArrayList.class.getName())) .append(annotationMetaEntity.importType(ArrayList.class.getName()))
.append("<>() {{\n\t\t\t\t") .append("<>() {{\n\t\t\t\t")
.append(paramName) .append(paramName)
.append(".forEach(sort -> add(") .append(".forEach(_sort -> add(")
.append("by(") .append("by(")
.append(annotationMetaEntity.importType(sortableEntityClass)) .append(annotationMetaEntity.importType(sortableEntityClass))
.append(".class, ") .append(".class, ")
.append("sort.property()") .append("_sort.property()")
.append(",\n\t\t\t\t\t\t") .append(",\n\t\t\t\t\t\t")
.append("sort.isAscending() ? ASCENDING : DESCENDING") .append("_sort.isAscending() ? ASCENDING : DESCENDING, ")
.append(", sort.ignoreCase())));\n\t\t\t}})"); .append("_sort.ignoreCase())));\n\t\t\t}})");
} }
} }
else if ( paramType.startsWith(JD_SORT) && paramType.endsWith("...") ) { else if ( paramType.startsWith(JD_SORT) && paramType.endsWith("...") ) {
@ -243,14 +243,14 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
declaration declaration
.append("\n\t\t\t.setOrder(asList(") .append("\n\t\t\t.setOrder(asList(")
.append(paramName) .append(paramName)
.append(").stream().map(sort -> ") .append(").stream().map(_sort -> ")
.append("by(") .append("by(")
.append(annotationMetaEntity.importType(sortableEntityClass)) .append(annotationMetaEntity.importType(sortableEntityClass))
.append(".class, ") .append(".class, ")
.append("sort.property()") .append("_sort.property()")
.append(",\n\t\t\t\t\t\t") .append(",\n\t\t\t\t\t\t")
.append("sort.isAscending() ? ASCENDING : DESCENDING") .append("_sort.isAscending() ? ASCENDING : DESCENDING, ")
.append(", sort.ignoreCase()))\n\t\t\t\t.collect(toList())\n\t\t\t)"); .append("_sort.ignoreCase()))\n\t\t\t\t.collect(toList())\n\t\t\t)");
} }
} }
else if ( paramType.startsWith(JD_SORT) ) { else if ( paramType.startsWith(JD_SORT) ) {

View File

@ -8,6 +8,7 @@ package org.hibernate.jpamodelgen.annotation;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.jpamodelgen.util.Constants; import org.hibernate.jpamodelgen.util.Constants;
import org.hibernate.query.NullPrecedence;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -258,32 +259,28 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
.append(')'); .append(')');
} }
private static final String ORDER_CONVERSION =
"builder.sort(entity.get(_sort.property())," +
"\n\t\t\t\t\t_sort.isAscending() ? ASCENDING : DESCENDING," +
"\n\t\t\t\t\tNONE, _sort.ignoreCase())";
private void orderBy(StringBuilder declaration, String paramName, boolean variadic) { private void orderBy(StringBuilder declaration, String paramName, boolean variadic) {
// TODO: Sort.ignoreCase() // TODO: Sort.ignoreCase()
if ( variadic ) { if ( variadic ) {
annotationMetaEntity.staticImport(Arrays.class.getName(), "asList"); annotationMetaEntity.staticImport(Arrays.class.getName(), "asList");
annotationMetaEntity.staticImport(Collectors.class.getName(), "toList"); annotationMetaEntity.staticImport(Collectors.class.getName(), "toList");
annotationMetaEntity.staticImport(NullPrecedence.class.getName(), "NONE");
declaration declaration
.append("\n\t\tasList(") .append("\n\t\tasList(")
.append(paramName) .append(paramName)
.append(")\n\t\t\t.stream()\n\t\t\t.map(sort -> sort.isAscending()\n") .append(")\n\t\t\t.stream()\n\t\t\t.map(_sort -> ")
.append("\t\t\t\t\t? builder.asc(entity.get(sort.property()))\n") .append(ORDER_CONVERSION)
.append("\t\t\t\t\t: builder.desc(entity.get(sort.property()))\n") .append("\n\t\t\t)\n\t\t\t.collect(toList())");
.append("\t\t\t)\n\t\t\t.collect(toList())");
} }
else { else {
declaration declaration
.append("\n\t\t") .append("\n\t\t")
.append(paramName) .append(ORDER_CONVERSION.replace("_sort", paramName));
.append(".isAscending()\n")
.append("\t\t\t\t? builder.")
.append("asc(entity.get(")
.append(paramName)
.append(".property()))\n")
.append("\t\t\t\t: builder.")
.append("desc(entity.get(")
.append(paramName)
.append(".property()))");
} }
} }