JPA: can't make OrderBy work

后端 未结 2 635
名媛妹妹
名媛妹妹 2021-01-21 11:24

I\'m trying to print ordered list after persisting it and retrieving.

My Entities:

@Entity
public class News {
    @Id @GeneratedValue
    private Long i         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-21 12:03

    @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.

提交回复
热议问题