1

I wondered how to do in JPA if I want to call a NamedQuery stored in a entity with different settings for example for order DESC or ASC to use in a way like for parameters (setParameter()). For example this is not working:

@NamedQuery(name = "Entry.findAll", query = "SELECT e FROM Entry e WHERE e.switch = :switch ORDER BY e.name :order)

Is really the only way to use two queries like:

@NamedQuery(name = "Entry.findAllDESC", query = "SELECT e FROM Entry e WHERE e.switch = :switch ORDER BY e.name DESC)
@NamedQuery(name = "Entry.findAllASC", query = "SELECT e FROM Entry e WHERE e.switch = :switch ORDER BY e.name ASC)

or to generate the query string in code?

I wondered that I found no way, because in most queries I have to change such things and this would make a programming style where I prefer to use central NamedQueries stored in the entities nearly impossible.

timmornYE
  • 708
  • 2
  • 8
  • 22
  • Possible duplicate + answers: http://stackoverflow.com/questions/4120388/hibernate-named-query-order-by-partameter – Magnilex Sep 04 '13 at 07:19

1 Answers1

0

NamedQuery is static query. You can't put dynamic query value (i.e ASC or DESC) in NamedQuery. You should use DynamicQuery(i.e. JPQL) or Native Query or Criteria to get your desired output.

Masudul
  • 21,823
  • 5
  • 43
  • 58