I\'m trying to print ordered list after persisting it and retrieving.
My Entities:
@Entity
public class News {
@Id @GeneratedValue
private Long i
@OrderBy
is applied when sql query is executed. In your case the data is already in memory so sql query is not executed. You can try using @Sort
annotation that apply sorting in memory. Depending on your use case it may not be efficient if your list is big.
@Sort(type = SortType.COMPARATOR, comparator = CommentsComparator.class)
EDIT: @Sort is a hibernate specific annotation. For pure JPA I think the collection (List) can be updated to some sorted collection like SortedSet if possible.