'Custom query with order by argument
I want to prepare custom query for getting sorted list of objectes from repository. I would like to have only one query, where I put only name of column as argument. What should I add after "Order by"?
@Query("FROM NoteEntity n where n.user.id = ?2 ORDER BY :columnName ASC")
List<NoteEntity> getNotesSortedByColumn(String columnName, String userID);
Following query return list of objects but without any sorting.
Solution 1:[1]
Ok, thanks for help. I used "sort" to the custom query and everythingworks. In repository:
@Query("FROM NoteEntity n where n.user.id = ?1")
List<NoteEntity> getNotesSortedBy(String userID, Sort sort);
and in service:
List<NoteEntity> sortedNotes = noteEntityRepository.getNotesSortedBy(
userID, Sort.by(Sort.Direction.ASC, "columnName"));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lulex97 |
