HHH-17772 tolerate vararg Sort parameters
This commit is contained in:
parent
df79c4491a
commit
2281805e91
|
@ -15,6 +15,7 @@ import org.hibernate.query.Page;
|
|||
import org.hibernate.query.SortDirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -209,29 +210,6 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
|
|||
|
||||
boolean setOrder(StringBuilder declaration, boolean unwrapped, String paramName, String paramType) {
|
||||
unwrapQuery( declaration, unwrapped );
|
||||
if ( paramType.startsWith(JD_SORT) ) {
|
||||
final String sortableEntityClass = getSortableEntityClass();
|
||||
if ( sortableEntityClass != null ) {
|
||||
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(sortableEntityClass))
|
||||
.append(".class, ")
|
||||
.append(paramName)
|
||||
.append(".property()")
|
||||
.append(",\n\t\t\t\t\t")
|
||||
.append(paramName)
|
||||
.append(".isAscending() ? ASCENDING : DESCENDING")
|
||||
.append("))");
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ( paramType.startsWith(JD_ORDER) ) {
|
||||
final String sortableEntityClass = getSortableEntityClass();
|
||||
if ( sortableEntityClass != null ) {
|
||||
|
@ -252,10 +230,46 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
|
|||
.append(",\n\t\t\t\t\t\t")
|
||||
.append("sort.isAscending() ? ASCENDING : DESCENDING")
|
||||
.append(", sort.ignoreCase())));\n\t\t\t}})");
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
else if ( paramType.startsWith(JD_SORT) && paramType.endsWith("...") ) {
|
||||
final String sortableEntityClass = getSortableEntityClass();
|
||||
if ( sortableEntityClass != null ) {
|
||||
annotationMetaEntity.staticImport(SortDirection.class.getName(), "ASCENDING");
|
||||
annotationMetaEntity.staticImport(SortDirection.class.getName(), "DESCENDING");
|
||||
annotationMetaEntity.staticImport(Arrays.class.getName(), "asList");
|
||||
annotationMetaEntity.staticImport(Order.class.getName(), "by");
|
||||
annotationMetaEntity.staticImport(Collectors.class.getName(), "toList");
|
||||
declaration
|
||||
.append("\n\t\t\t.setOrder(asList(")
|
||||
.append(paramName)
|
||||
.append(").stream().map(sort -> ")
|
||||
.append("by(")
|
||||
.append(annotationMetaEntity.importType(sortableEntityClass))
|
||||
.append(".class, ")
|
||||
.append("sort.property()")
|
||||
.append(",\n\t\t\t\t\t\t")
|
||||
.append("sort.isAscending() ? ASCENDING : DESCENDING")
|
||||
.append(", sort.ignoreCase()))\n\t\t\t\t.collect(toList())\n\t\t\t)");
|
||||
}
|
||||
}
|
||||
else if ( paramType.startsWith(JD_SORT) ) {
|
||||
final String sortableEntityClass = getSortableEntityClass();
|
||||
if ( sortableEntityClass != null ) {
|
||||
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(sortableEntityClass))
|
||||
.append(".class, ")
|
||||
.append(paramName)
|
||||
.append(".property()")
|
||||
.append(",\n\t\t\t\t\t")
|
||||
.append(paramName)
|
||||
.append(".isAscending() ? ASCENDING : DESCENDING")
|
||||
.append("))");
|
||||
}
|
||||
}
|
||||
else if ( paramType.endsWith("...") ) {
|
||||
|
|
|
@ -9,8 +9,10 @@ package org.hibernate.jpamodelgen.annotation;
|
|||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.hibernate.jpamodelgen.util.Constants;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.hibernate.jpamodelgen.util.Constants.JD_SORT;
|
||||
import static org.hibernate.jpamodelgen.util.TypeUtils.isPrimitive;
|
||||
|
@ -190,6 +192,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
|||
for ( int i = 0; i < paramNames.size(); i ++ ) {
|
||||
final String paramName = paramNames.get(i);
|
||||
final String paramType = paramTypes.get(i);
|
||||
//TODO: Jakarta Order!!
|
||||
if ( isJakartaSortParam(paramType) ) {
|
||||
if ( firstOrderBy ) {
|
||||
firstOrderBy = false;
|
||||
|
@ -197,7 +200,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
|||
else {
|
||||
declaration.append(',');
|
||||
}
|
||||
orderBy(declaration, paramName);
|
||||
orderBy(declaration, paramName, paramType.endsWith("..."));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -255,8 +258,20 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
|||
.append(')');
|
||||
}
|
||||
|
||||
private static void orderBy(StringBuilder declaration, String paramName) {
|
||||
private void orderBy(StringBuilder declaration, String paramName, boolean variadic) {
|
||||
// TODO: Sort.ignoreCase()
|
||||
if ( variadic ) {
|
||||
annotationMetaEntity.staticImport(Arrays.class.getName(), "asList");
|
||||
annotationMetaEntity.staticImport(Collectors.class.getName(), "toList");
|
||||
declaration
|
||||
.append("\n\t\tasList(")
|
||||
.append(paramName)
|
||||
.append(")\n\t\t\t.stream()\n\t\t\t.map(sort -> sort.isAscending()\n")
|
||||
.append("\t\t\t\t\t? builder.asc(entity.get(sort.property()))\n")
|
||||
.append("\t\t\t\t\t: builder.desc(entity.get(sort.property()))\n")
|
||||
.append("\t\t\t)\n\t\t\t.collect(toList())");
|
||||
}
|
||||
else {
|
||||
declaration
|
||||
.append("\n\t\t")
|
||||
.append(paramName)
|
||||
|
@ -270,6 +285,7 @@ public class CriteriaFinderMethod extends AbstractFinderMethod {
|
|||
.append(paramName)
|
||||
.append(".property()))");
|
||||
}
|
||||
}
|
||||
|
||||
private void parameter(StringBuilder declaration, int i, String paramName, String paramType) {
|
||||
declaration
|
||||
|
|
|
@ -47,6 +47,9 @@ public interface BookAuthorRepository {
|
|||
@Find
|
||||
List<Book> byPubDate2(LocalDate publicationDate, Order<? super Book> order);
|
||||
|
||||
@Find
|
||||
List<Book> byPubDate3(LocalDate publicationDate, Sort<? super Book>... order);
|
||||
|
||||
@Insert
|
||||
void create(Book book);
|
||||
|
||||
|
@ -70,4 +73,7 @@ public interface BookAuthorRepository {
|
|||
|
||||
@Query("from Book where title like :title")
|
||||
List<Book> books2(@Param("title") String titlePattern, Limit limit);
|
||||
|
||||
@Query("from Book where title like :title")
|
||||
List<Book> books3(String title, Limit limit, Sort<Book>... order);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue