Hi Guys,
I am trying to set the order of a query using an Expression, but
creiteria.orderAsc only supports SingularAttribute.

The use case is very simple:

1. I have a field in a legacy database, the field type is VARCHAR
2. The field only contains numbers, some of them with leading zeros.
3. I need to sort by that field
4. Because the field is VARCHAR, order is Alphanum, but I need to sort it
numerically
5. The native SQL solution is obvious (convert to int):
     ORDER BY myfunction(e.account_ident_)

6. I am using CriteriaSupport to create the query (complex conditions,
joins, etc...)

7. The sort part of the code is:

  public List<AccountEntity> search(DefaultAccountFilter filter) {
    return searchCriteria(filter)
    .orderAsc(AccountEntity_.accountIdent)
    .orderAsc(AccountEntity_.name)
    .orderDesc(AccountEntity_.id)
    .createQuery()
    .setFirstResult(filter.getPageStart())
    .setMaxResults(filter.getPageSize())
    .getResultList();
  }

As you can see, the problematic part is:

    .orderAsc(AccountEntity_.accountIdent)

How can I do something similar to:

    CriteriaBuilder cb = ...
    .orderAsc( cb.function("myfunction",
Integer.class, AccountEntity_.accountIdent) )

orderAsc/Desc only receives SingularAttribute, a future enhancement could
be to overload to receive Expression too.

Do you know any workaround I can do with the current version 1.9.0?

Thanks,

Frank.

-- 
Frank D. Martínez M.

Reply via email to