@OrderBy not working properly in JPA

前端 未结 2 1639
日久生厌
日久生厌 2021-01-18 23:39

How does @OrderBy work?

It is not working here in the following code:

Employee.java

package com.semanticbits.po         


        
相关标签:
2条回答
  • 2021-01-19 00:11

    I think you're misunderstanding what the @Orderby annotation actually does. According to the javadoc:

    Specifies the ordering of the elements of a collection valued association or element collection at the point when the association or collection is retrieved.

    [emphasis added] The annotation does not dictate insertion order. Continuing with your example, if you were to fetch an Employee:

    Employee employee = manager.find(Employee.class, employeeId);
    List<Address> addresses = employee.getAddress(); 
    

    Then addresses would be sorted by city in descending order.

    0 讨论(0)
  • 2021-01-19 00:16

    according to the spec you would have to use:

    @OrderBy("address.city DESC") 
    
    0 讨论(0)
提交回复
热议问题