@@ -321,14 +321,30 @@ public Page<ContingencyEntity> findContingenciesPage(UUID resultUuid, UUID netwo
321321 // HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!
322322 // cf. https://vladmihalcea.com/fix-hibernate-hhh000104-entity-fetch-pagination-warning-message/
323323 // We must separate in two requests, one with pagination the other one with Join Fetch
324+ // Determine which properties to project based on sort fields
325+ // When using DISTINCT, all ORDER BY columns must be in the SELECT list
326+ List <String > projectionProperties = new ArrayList <>();
327+ projectionProperties .add ("uuid" );
328+
329+ // Add sort properties to projection to satisfy DISTINCT + ORDER BY requirement
330+ for (Sort .Order order : modifiedPageable .getSort ()) {
331+ String property = order .getProperty ();
332+ if (!property .equals ("uuid" ) && !projectionProperties .contains (property )) {
333+ projectionProperties .add (property );
334+ }
335+ }
324336
325337 // First, we fetch contingencies UUIDs, with all the filters and pagination
326- Page <ContingencyRepository .EntityUuid > uuidPage = contingencyRepository .findBy (specification , q ->
327- q .project ("uuid" )
328- .as (ContingencyRepository .EntityUuid .class )
329- .sortBy (modifiedPageable .getSort ())
330- .page (modifiedPageable )
331- );
338+ Page <ContingencyRepository .EntityUuid > uuidPage = contingencyRepository .findBy (specification , q -> {
339+ var query = q .as (ContingencyRepository .EntityUuid .class )
340+ .sortBy (modifiedPageable .getSort ());
341+ if (projectionProperties .size () == 1 ) {
342+ query = query .project ("uuid" );
343+ } else {
344+ query = query .project (projectionProperties .toArray (new String [0 ]));
345+ }
346+ return query .page (modifiedPageable );
347+ });
332348
333349 if (!uuidPage .hasContent ()) {
334350 // Since springboot 3.2, the return value of Page.empty() is not serializable. See https://github.com/spring-projects/spring-data-commons/issues/2987
@@ -365,12 +381,30 @@ public Page<SubjectLimitViolationEntity> findSubjectLimitViolationsPage(UUID res
365381 // HHH000104: firstResult/maxResults specified with collection fetch; applying in memory!
366382 // cf. https://vladmihalcea.com/fix-hibernate-hhh000104-entity-fetch-pagination-warning-message/
367383 // We must separate in two requests, one with pagination the other one with Join Fetch
368- Page <SubjectLimitViolationRepository .EntityId > uuidPage = subjectLimitViolationRepository .findBy (specification , q ->
369- q .project ("id" )
370- .as (SubjectLimitViolationRepository .EntityId .class )
371- .sortBy (modifiedPageable .getSort ())
372- .page (modifiedPageable )
373- );
384+
385+ // Determine which properties to project based on sort fields
386+ // When using DISTINCT, all ORDER BY columns must be in the SELECT list
387+ List <String > projectionProperties = new ArrayList <>();
388+ projectionProperties .add ("id" );
389+
390+ // Add sort properties to projection to satisfy DISTINCT + ORDER BY requirement
391+ for (Sort .Order order : modifiedPageable .getSort ()) {
392+ String property = order .getProperty ();
393+ if (!property .equals ("id" ) && !projectionProperties .contains (property )) {
394+ projectionProperties .add (property );
395+ }
396+ }
397+
398+ Page <SubjectLimitViolationRepository .EntityId > uuidPage = subjectLimitViolationRepository .findBy (specification , q -> {
399+ var query = q .as (SubjectLimitViolationRepository .EntityId .class )
400+ .sortBy (modifiedPageable .getSort ());
401+ if (projectionProperties .size () == 1 ) {
402+ query = query .project ("id" );
403+ } else {
404+ query = query .project (projectionProperties .toArray (new String [0 ]));
405+ }
406+ return query .page (modifiedPageable );
407+ });
374408
375409 if (!uuidPage .hasContent ()) {
376410 // Since springboot 3.2, the return value of Page.empty() is not serializable. See https://github.com/spring-projects/spring-data-commons/issues/2987
0 commit comments