示例#1
0
  /**
   * Creates a criteria API {@link javax.persistence.criteria.Order} from the given {@link Order}.
   *
   * @param order the order to transform into a JPA {@link javax.persistence.criteria.Order}
   * @param root the {@link Root} the {@link Order} expression is based on
   * @param cb the {@link CriteriaBuilder} to build the {@link javax.persistence.criteria.Order}
   *     with
   * @return
   */
  @SuppressWarnings("unchecked")
  private static javax.persistence.criteria.Order toJpaOrder(
      Order order, Root<?> root, CriteriaBuilder cb) {

    PropertyPath property = PropertyPath.from(order.getProperty(), root.getJavaType());
    Expression<?> expression = toExpressionRecursively(root, property);

    if (order.isIgnoreCase() && String.class.equals(expression.getJavaType())) {
      Expression<String> lower = cb.lower((Expression<String>) expression);
      return order.isAscending() ? cb.asc(lower) : cb.desc(lower);
    } else {
      return order.isAscending() ? cb.asc(expression) : cb.desc(expression);
    }
  }
示例#2
0
  /**
   * Returns the order clause for the given {@link Order}. Will prefix the clause with the given
   * alias if the referenced property refers to a join alias.
   *
   * @param joinAliases the join aliases of the original query.
   * @param alias the alias for the root entity.
   * @param order the order object to build the clause for.
   * @return
   */
  private static String getOrderClause(Set<String> joinAliases, String alias, Order order) {

    String property = order.getProperty();
    boolean qualifyReference = !property.contains("("); // ( indicates a function

    for (String joinAlias : joinAliases) {
      if (property.startsWith(joinAlias)) {
        qualifyReference = false;
        break;
      }
    }

    String reference = qualifyReference ? String.format("%s.%s", alias, property) : property;
    String wrapped = order.isIgnoreCase() ? String.format("lower(%s)", reference) : reference;

    return String.format("%s %s", wrapped, toJpaDirection(order));
  }