0

How can I use the "order by" clause with "asc nulls first"?

This is my code:

CriteriaBuilder builder = em.getCriteriaBuilder();

CriteriaQuery<MyClassModel> query = builder.createQuery(MyClassModel.class);

//esprToOrder is a Expression<?> istance that containing the sort field...
query.orderBy(builder.asc(esprToOrder));

TypedQuery<MyClassModel> myQ = em.createQuery(query);

List<MyClassModel> myList = myQ.getResultList();

myList doesn't contain any records with null field sort...

perissf
  • 15,979
  • 14
  • 80
  • 117
bancomat
  • 15
  • 6
  • 1
    Welcome to SO. I have formatted your code. [Check out](http://stackoverflow.com/posts/24762045/edit) how I did - basically 4 spaces for code blocks. See also [here](http://stackoverflow.com/help/asking) for more help about how to ask. What do you mean with _myList doesn't contain any records with null field sort_? – perissf Jul 15 '14 at 15:31
  • Hi perissf, thanks. myList hasn't the records with sort field null value. – bancomat Jul 15 '14 at 15:37

1 Answers1

4

JPA doesn't support specification of "NULLS FIRST|LAST". Some implementations may allow it (by casting to allow extra methods), but it's not part of the JPA spec.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29