Skip to content

Commit

Permalink
Merge branch 'sbearcsiro-bugfix/594' into 7.3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Puneet Behl committed Nov 15, 2022
2 parents 2e2fd44 + 97a06bf commit 3a9d674
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.grails.orm.hibernate.query;

import org.grails.datastore.mapping.config.Property;
import org.grails.datastore.mapping.reflect.ClassUtils;
import org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder;
import org.grails.orm.hibernate.cfg.Mapping;
Expand Down Expand Up @@ -272,21 +273,22 @@ private static void addOrderPossiblyNested(CriteriaQuery query,
String sort,
String order,
boolean ignoreCase) {
if (ignoreCase && entity.getPropertyByName(sort).getType() != String.class) {
ignoreCase = false;
}
int firstDotPos = sort.indexOf(".");
if (firstDotPos == -1) {
final PersistentProperty<? extends Property> property = entity.getPropertyByName(sort);
ignoreCase = isIgnoreCaseProperty(ignoreCase, property);
addOrder(entity, query, queryRoot, criteriaBuilder, sort, order, ignoreCase);
} else { // nested property
String sortHead = sort.substring(0, firstDotPos);
String sortTail = sort.substring(firstDotPos + 1);
PersistentProperty property = entity.getPropertyByName(sortHead);
final PersistentProperty<? extends Property> property = entity.getPropertyByName(sortHead);
if (property instanceof Embedded) {
// embedded objects cannot reference entities (at time of writing), so no more recursion needed
final PersistentProperty<? extends Property> associatedProperty = ((Embedded<?>) property).getAssociatedEntity().getPropertyByName(sortTail);
ignoreCase = isIgnoreCaseProperty(ignoreCase, associatedProperty);
addOrder(entity, query, queryRoot, criteriaBuilder, sort, order, ignoreCase);
} else if (property instanceof Association) {
Association a = (Association) property;
final Association<? extends Property> a = (Association<? extends Property>) property;
final Join join = queryRoot.join(sortHead);
PersistentEntity associatedEntity = a.getAssociatedEntity();
Class<?> propertyTargetClass = associatedEntity.getJavaClass();
Expand All @@ -295,6 +297,13 @@ private static void addOrderPossiblyNested(CriteriaQuery query,
}
}

private static boolean isIgnoreCaseProperty(boolean ignoreCase, PersistentProperty<? extends Property> persistentProperty) {
if (ignoreCase && persistentProperty != null && persistentProperty.getType() != String.class) {
ignoreCase = false;
}
return ignoreCase;
}

/**
* Add order directly to criteria.
*/
Expand Down

0 comments on commit 3a9d674

Please sign in to comment.