code: static <E extends Entity> String getSqlSort(EntityType entityType, Query<E> q) {
      StringBuilder sortSql = new StringBuilder();
  
      // https://www.postgresql.org/docs/9.6/static/queries-limit.html
      // When using LIMIT, it is important to use an ORDER BY clause that constrains the result rows
      // into a unique order.
      // Otherwise you will get an unpredictable subset of the query's rows. You might be asking for
      // the tenth through twentieth rows,
      // but tenth through twentieth in what ordering? The ordering is unknown, unless you specified
      // ORDER BY.
      Sort sort;
      if (q.getSort() != null && !hasUniqueSortAttribute(entityType, q.getSort())) {
        LOG.debug("Query with sort without unique attribute detected: {}", q);
        sort = new Sort(q.getSort());
        sort.on(entityType.getIdAttribute().getName());
      } else if (q.getSort() == null) {
        LOG.debug("Query without sort detected: {}", q);
        sort = new Sort(entityType.getIdAttribute().getName());
      } else {
        sort = q.getSort();
      }
  
      for (Sort.Order o : sort) {
        Attribute attr = entityType.getAttribute(o.getAttr());
        sortSql.append(", ").append(getColumnName(attr));
        if (o.getDirection().equals(Sort.Direction.DESC)) {
          sortSql.append(" DESC");
        } else {
          sortSql.append(" ASC");
        }
      }
  
      if (sortSql.length() > 0) {
        sortSql = new StringBuilder("ORDER BY ").append(sortSql.substring(2));
      }
  
      return sortSql.toString();
    }
docs: Package-private for testability