Dynamic ordering in JPA / Hibernate?

A naive attempt request would look like this:

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

This does not work, since the parameters must be data, not column names or syntax like ASC or DESC.

What workarounds have you developed for this dynamic order? Concatenating the order line to the query line is obvious, but are there any better ones?

+3
source share
1 answer

This, unfortunately, is how it should be done with JPA.

, , , (? 1,? 2..) (em.createNativeQuery) Hibernate ( ). ( JPA2 Hibernate) API

+3

Source: https://habr.com/ru/post/1728285/


All Articles