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