views:

553

answers:

1

The naive attempt query would look like this:

Query query = em.createQuery("from org.domain.Resource r where r._parent = ? order by ?");

This does not work as the parameters should be data and not column names or syntax like ASC or DESC.

What kind of workarounds you have figured out for this dynamic ordering? Concatenating the ordering string to the query string is the obvious one but are there any better ones?

+1  A: 

This is unfortunately how it must be done with JPA.

I do however vaguely remember having used numbered parameters (?1, ?2..) for ordering with native queries (em.createNativeQuery) on Hibernate (but I could be wrong). Anyway another solution (if you are using JPA2 or Hibernate) is to use the criteria API

Lars Tackmann