Skip to content

Commit

Permalink
DATAJPA-12 - More correct interpretation of setting sort paths.
Browse files Browse the repository at this point in the history
We now treat paths handed into a Sort object as individual property traversals and build and join them together appropriately. Thx to Nemanja Nedic for the contribution.
  • Loading branch information
odrotbohm committed Mar 11, 2011
1 parent 3128fe2 commit 730e688
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -311,7 +312,21 @@ public static List<javax.persistence.criteria.Order> toOrders(Sort sort,
private static javax.persistence.criteria.Order toJpaOrder(Order order,
Root<?> root, CriteriaBuilder cb) {

Expression<?> expression = root.get(order.getProperty());
Expression<?> expression = null;
String pathString = order.getProperty();
String[] pathElements = pathString.split("\\.");
int pathSize = pathElements.length;

if (pathSize > 1) {
Join<Object, Object> path = root.join(pathElements[0]);
for (int i = 1; i < pathSize - 1; i++) {
path = path.join(pathElements[i]);
}
expression = path.get(pathElements[pathSize - 1]);
} else {
expression = root.get(pathString);
}

return order.isAscending() ? cb.asc(expression) : cb.desc(expression);
}
}

0 comments on commit 730e688

Please sign in to comment.