diff --git a/grails-datastore-gorm-hibernate/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java b/grails-datastore-gorm-hibernate/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java index f586c8cf611..398cb40b771 100644 --- a/grails-datastore-gorm-hibernate/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java +++ b/grails-datastore-gorm-hibernate/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java @@ -1575,16 +1575,18 @@ else if (paginationEnabledList) { // Restore the previous projection, add settings for the pagination parameters, // and then execute the query. - if (projectionList != null && projectionList.getLength() > 0) { - criteria.setProjection(projectionList); - } else { - criteria.setProjection(null); - } + boolean isProjection = (projectionList != null && projectionList.getLength() > 0); + criteria.setProjection(isProjection ? projectionList : null); + for (Order orderEntry : orderEntries) { criteria.addOrder(orderEntry); } if (resultTransformer == null) { - criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); + // GRAILS-9644 - Use projection transformer + criteria.setResultTransformer( isProjection ? + CriteriaSpecification.PROJECTION : + CriteriaSpecification.ROOT_ENTITY + ); } else if (paginationEnabledList) { // relevant to GRAILS-5692 diff --git a/grails-datastore-gorm-hibernate4/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java b/grails-datastore-gorm-hibernate4/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java index f822c6c91b6..ac70d2fa68b 100644 --- a/grails-datastore-gorm-hibernate4/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java +++ b/grails-datastore-gorm-hibernate4/src/main/groovy/grails/orm/HibernateCriteriaBuilder.java @@ -1575,16 +1575,18 @@ else if (paginationEnabledList) { // Restore the previous projection, add settings for the pagination parameters, // and then execute the query. - if (projectionList != null && projectionList.getLength() > 0) { - criteria.setProjection(projectionList); - } else { - criteria.setProjection(null); - } + boolean isProjection = (projectionList != null && projectionList.getLength() > 0); + criteria.setProjection(isProjection ? projectionList : null); + for (Order orderEntry : orderEntries) { criteria.addOrder(orderEntry); } if (resultTransformer == null) { - criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); + // GRAILS-9644 - Use projection transformer + criteria.setResultTransformer( isProjection ? + CriteriaSpecification.PROJECTION : + CriteriaSpecification.ROOT_ENTITY + ); } else if (paginationEnabledList) { // relevant to GRAILS-5692