1717
1818import static org .springframework .data .jdbc .repository .query .JdbcQueryExecution .*;
1919
20- import java .lang .reflect .Field ;
2120import java .sql .ResultSet ;
2221import java .util .ArrayList ;
23- import java .util .Arrays ;
2422import java .util .Collection ;
2523import java .util .LinkedHashMap ;
2624import java .util .List ;
2927import java .util .function .IntFunction ;
3028import java .util .function .LongSupplier ;
3129import java .util .function .Supplier ;
32- import java .util .stream .Collectors ;
3330
3431import org .jspecify .annotations .Nullable ;
3532import org .springframework .core .convert .converter .Converter ;
4441import org .springframework .data .domain .Window ;
4542import org .springframework .data .jdbc .core .JdbcAggregateOperations ;
4643import org .springframework .data .jdbc .core .convert .JdbcConverter ;
44+ import org .springframework .data .mapping .PersistentPropertyAccessor ;
4745import org .springframework .data .relational .core .conversion .RelationalConverter ;
4846import org .springframework .data .relational .core .dialect .Dialect ;
4947import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
6462import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
6563import org .springframework .jdbc .core .namedparam .SqlParameterSource ;
6664import org .springframework .util .Assert ;
67- import org .springframework .util .ReflectionUtils ;
6865
6966/**
7067 * An {@link AbstractJdbcQuery} implementation based on a {@link PartTree}.
@@ -318,19 +315,17 @@ static class ScrollQueryExecution<T> implements JdbcQueryExecution<Window<T>> {
318315 if (orders .isEmpty ())
319316 orders = sort .get ().map (Sort .Order ::getProperty ).toList ();
320317
321- orders = orders . stream (). map ( it -> {
322- RelationalPersistentProperty prop = tableEntity . getPersistentProperty ( it );
323-
318+ List < RelationalPersistentProperty > properties = new ArrayList <>();
319+ for ( String propertyName : orders ) {
320+ RelationalPersistentProperty prop = tableEntity . getPersistentProperty ( propertyName );
324321 if (prop == null )
325- return it ;
326-
327- return prop .getName ();
328- }).toList ();
322+ continue ;
329323
330- keys = extractKeys (resultList , orders );
324+ properties .add (prop );
325+ }
331326
332- Map <String , Object > finalKeys = keys ;
333- positionFunction = (ignoredI ) -> ScrollPosition .of (finalKeys , ((KeysetScrollPosition ) position ).getDirection ());
327+ final Map <String , Object > resultKeys = extractKeys ( resultList , properties ) ;
328+ positionFunction = (ignoredI ) -> ScrollPosition .of (resultKeys , ((KeysetScrollPosition ) position ).getDirection ());
334329 }
335330
336331 if (positionFunction == null )
@@ -347,26 +342,26 @@ else if (limit.isLimited())
347342 return Window .from (resultList , positionFunction , hasNext );
348343 }
349344
350- private Map <String , Object > extractKeys (List <T > resultList , List <String > orders ) {
345+ private Map <String , Object > extractKeys (List <T > resultList , List <RelationalPersistentProperty > properties ) {
351346 if (resultList .isEmpty ())
352347 return Map .of ();
353348
349+ Map <String , Object > result = new LinkedHashMap <>();
350+
354351 T last = resultList .get (resultList .size () - 1 );
352+ PersistentPropertyAccessor <T > accessor = tableEntity .getPropertyAccessor (last );
355353
356- Field [] fields = last .getClass ().getDeclaredFields ();
354+ for (RelationalPersistentProperty property : properties ) {
355+ String propertyName = property .getName ();
356+ Object propertyValue = accessor .getProperty (property );
357357
358- // noinspection DataFlowIssue
359- return Arrays .stream (fields ).filter (it -> {
360- String name = it .getName ();
358+ if (propertyValue == null )
359+ continue ;
361360
362- RelationalPersistentProperty prop = tableEntity .getPersistentProperty (name );
363- if (prop != null )
364- name = prop .getName ();
361+ result .put (propertyName , propertyValue );
362+ }
365363
366- String finalName = name ;
367- return orders .stream ().anyMatch (order -> order .equalsIgnoreCase (finalName ));
368- }).peek (ReflectionUtils ::makeAccessible ).collect (Collectors .toMap (Field ::getName ,
369- it -> ReflectionUtils .getField (it , last ), (e1 , e2 ) -> e1 , LinkedHashMap ::new ));
364+ return result ;
370365 }
371366 }
372367
0 commit comments