@@ -94,6 +94,11 @@ public String getPropertyName() {
9494
9595 abstract boolean isNullable (int index );
9696
97+ boolean initiallyUnwrapped () {
98+ return !isUsingEntityManager () // a TypedQuery from EntityManager is not a SelectionQuery
99+ || isUsingSpecification () && !isReactive (); // SelectionSpecification.createQuery() returns SelectionQuery
100+ }
101+
97102 List <String > parameterTypes () {
98103 return paramTypes .stream ()
99104 .map (paramType -> isOrderParam (paramType ) && paramType .endsWith ("[]" )
@@ -275,19 +280,6 @@ else if ( jakartaPageRequest ) {
275280 }
276281 }
277282
278- void handleSorting (
279- StringBuilder declaration , List <String > paramTypes ,
280- @ Nullable String containerType ) {
281- if ( !isJakartaCursoredPage (containerType )
282- && hasOrdering (paramTypes ) ) {
283- if ( !isUsingSpecification () ) {
284- throw new AssertionError ();
285- }
286- declaration
287- .append ("\t _spec.resort(_orders);\n " );
288- }
289- }
290-
291283 void handlePageParameters (
292284 StringBuilder declaration , List <String > paramTypes ,
293285 @ Nullable String containerType ) {
@@ -547,22 +539,34 @@ private void totalResults(StringBuilder declaration, List<String> paramTypes) {
547539 }
548540 }
549541
550- void collectOrdering (StringBuilder declaration , List <String > paramTypes ) {
542+ void collectOrdering (StringBuilder declaration , List <String > paramTypes , @ Nullable String containerType ) {
551543 if ( hasOrdering (paramTypes ) ) {
552544 if ( returnTypeName != null ) {
553- declaration
554- .append ("\t var _orders = new " )
555- .append (annotationMetaEntity .importType ("java.util.ArrayList" ))
556- .append ("<" )
557- .append (annotationMetaEntity .importType (HIB_ORDER ))
558- .append ("<? super " )
559- .append (annotationMetaEntity .importType (returnTypeName ))
560- .append (">>();\n " );
545+ final boolean cursoredPage = isJakartaCursoredPage ( containerType );
546+ final String add ;
547+ if ( cursoredPage ) {
548+ // we need to collect them together in a List
549+ declaration
550+ .append ("\t var _orders = new " )
551+ .append (annotationMetaEntity .importType ("java.util.ArrayList" ))
552+ .append ("<" )
553+ .append (annotationMetaEntity .importType (HIB_ORDER ))
554+ .append ("<? super " )
555+ .append (annotationMetaEntity .importType (returnTypeName ))
556+ .append (">>();\n " );
557+ add = "_orders.add" ;
558+ }
559+ else {
560+ add = "_spec.sort" ;
561+ }
562+
561563 // static orders declared using @OrderBy must come first
562564 for ( OrderBy orderBy : orderBys ) {
563565 annotationMetaEntity .staticImport (HIB_SORT_DIRECTION , "*" );
564566 declaration
565- .append ("\t _orders.add(" )
567+ .append ("\t " )
568+ .append (add )
569+ .append ('(' )
566570 .append (annotationMetaEntity .staticImport (HIB_ORDER , orderBy .descending ? "desc" : "asc" ))
567571 .append ('(' )
568572 .append (annotationMetaEntity .importType (returnTypeName ))
@@ -585,28 +589,46 @@ void collectOrdering(StringBuilder declaration, List<String> paramTypes) {
585589 .append ("\t for (var _sort : " )
586590 .append (name )
587591 .append (") {\n " )
588- .append ("\t \t _orders.add(_sort);\n " )
592+ .append ("\t \t " )
593+ .append (add )
594+ .append ("(_sort);\n " )
589595 .append ("\t }\n " );
590596 }
591597 else if ( type .startsWith (HIB_ORDER ) ) {
592598 declaration
593- .append ("\t _orders.add(" )
599+ .append ("\t " )
600+ .append (add )
601+ .append ('(' )
594602 .append (name )
595603 .append (");\n " );
596604 }
597605 else if ( type .startsWith (LIST + "<" + HIB_ORDER ) ) {
598- declaration
599- .append ("\t _orders.addAll(" )
600- .append (name )
601- .append (");\n " );
606+ if ( cursoredPage ) {
607+ declaration
608+ .append ("\t _orders.addAll(" )
609+ .append (name )
610+ .append (");\n " );
611+ }
612+ else {
613+ declaration
614+ .append ("\t for (var _sort : " )
615+ .append (name )
616+ .append (") {\n " )
617+ .append ("\t \t " )
618+ .append (add )
619+ .append ("(_sort);\n " )
620+ .append ("\t }\n " );
621+ }
602622 }
603623 else if ( type .startsWith (JD_ORDER ) ) {
604624 annotationMetaEntity .staticImport (HIB_SORT_DIRECTION , "*" );
605625 declaration
606626 .append ("\t for (var _sort : " )
607627 .append (name )
608628 .append (".sorts()) {\n " )
609- .append ("\t \t _orders.add(" )
629+ .append ("\t \t " )
630+ .append (add )
631+ .append ('(' )
610632 .append (annotationMetaEntity .staticImport (HIB_ORDER , "asc" ))
611633 .append ('(' )
612634 .append (annotationMetaEntity .importType (returnTypeName ))
@@ -624,7 +646,9 @@ else if ( type.startsWith(JD_SORT) && type.endsWith("...") ) {
624646 .append ("\t for (var _sort : " )
625647 .append (name )
626648 .append (") {\n " )
627- .append ("\t \t _orders.add(" )
649+ .append ("\t \t " )
650+ .append (add )
651+ .append ('(' )
628652 .append (annotationMetaEntity .staticImport (HIB_ORDER , "asc" ))
629653 .append ('(' )
630654 .append (annotationMetaEntity .importType (returnTypeName ))
@@ -638,7 +662,9 @@ else if ( type.startsWith(JD_SORT) && type.endsWith("...") ) {
638662 else if ( type .startsWith (JD_SORT ) ) {
639663 annotationMetaEntity .staticImport (HIB_SORT_DIRECTION , "*" );
640664 declaration
641- .append ("\t _orders.add(" )
665+ .append ("\t " )
666+ .append (add )
667+ .append ('(' )
642668 .append (annotationMetaEntity .staticImport (HIB_ORDER , "asc" ))
643669 .append ('(' )
644670 .append (annotationMetaEntity .importType (returnTypeName ))
0 commit comments