I have
Entity: Users
and
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u")
Use it like this:
Query query = em.createNamedQuery("Technologies.findByIsActive", Technologies.class);
query.setFirstResult(technologiesTableModel.getLowItemIndex());
query.setMaxResults(technologiesTableModel.getPageRange().intValue());
List<Technologies> technologies = query.setParameter("isActive", 'Y').getResultList();
But I want to sort this result dynamic. I have one String that contains column name that I want to sort(field content come from request). How can I do that without new query for each field and direction and to type safe (without direct append order by clause with field name).
My research show suggestion to use CriteriaQuery(I can't found example with using a NamedQuery defined above entity), TypedQuery(I can't found example with ordering, only with setFirstResult and setMaxResults. But I need both sorting and pagination) or JpaRepository (I can't find full implemented example with good description).
That I want is to sort my data by field name (that I have like String) and after that to paging it (with setFirstResult and setMaxResults). I want to do that with NamedQuery that I was define(in Entity class declaration) and not define query on fly or append order clause to query end with String concatenations.
EDIT:
All I need is a good explanation how this have to be implemented (with all good practices). I didn't have idea how to do that. All I try is:
- My queries to be at one place(most of them)
- String concatenation is not good practice to create queries.