简体   繁体   中英

Spring Data REST: sort parameter is ignored

Spring Boot 2.0.1.RELEASE project with Spring Data JPA and Spring Data REST. It seems that the sort parameter in the REST endpoint is ignored (but the same repository method do work in a unit test). The repository is the following:

@RepositoryRestResource(collectionResourceRel = "orders", path = "orders")
public interface OrderRepository extends PagingAndSortingRepository<OrderEntity, Integer>, OrderRepositoryExtended {

@Query(value = "FROM OrderEntity a WHERE "
                + " (a.orderDateTime BETWEEN :dateFrom AND :dateTo) AND"
                + " ("
                + "  :searchTerm IS NULL OR"
                + "  (LOWER(a.customer.companyName) LIKE '%' || LOWER(:searchTerm) || '%') OR"
                + "  (LOWER(a.orderCode) LIKE '%' || LOWER(:searchTerm) || '%')"
                + " )"
)
Page<OrderEntity> findByOrderDateTimeBetweenAndSearchTerm(
        @RequestParam(name = "dateFrom")
        @Param("dateFrom")
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
            LocalDateTime dateFrom,

        @RequestParam(name = "dateTo")
        @Param("dateTo")
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
            LocalDateTime dateTo,

        @RequestParam(name = "searchTerm")
        @Param("searchTerm")
            String searchTerm,

        Pageable pageable);

}

The entity (edited) is as follows:

@Entity
@Table(name = "T_ORDERS")
@Data @NoArgsConstructor
@Cacheable(false)
public class OrderEntity {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    @Column(name = "ID_ORDER")
    @JsonProperty("id_order")
    private int id;

    @Column(name = "DATE_ORDER")
    @JsonProperty("date_order")
    private LocalDateTime orderDateTime;

}

When I try to invoke it using the exported REST endpoint, for example:

http://localhost:8080/api/v1/orders/search/findByOrderDateTimeBetweenAndSearchTerm?dateFrom=2018-01-01T00:00:00&dateTo=2018-12-01T00:00:00&sort=orderDateTime&searchTerm=O-2018

The sort parameter is ignored as you can see in the generated query (edited for clarity):

select orderentit0_.id_order as id_order1_21_
...
from t_orders orderentit0_ 
cross join t_customers customeren1_ 
where 
    orderentit0_.id_customer=customeren1_.id_customer and 
    (orderentit0_.date_order between ? and ?) and 
    (? is null or lower(customeren1_.company_name) like concat('%'
lower(?)
'%') or lower(orderentit0_.order_code) like concat('%'
lower(?)
'%')) limit ?

I've already tried simplifing the WHERE expression removing the LIKE conditions but with no luck.

Thanks!

The 'exported' name of your property is date_order . It should be used this way not only in json requests but also in request parameters.

@Column(name = "DATE_ORDER")
@JsonProperty("date_order")
private LocalDateTime orderDateTime;

Try this way:

http://localhost:8080/api/v1/orders/search/findByOrderDateTimeBetweenAndSearchTerm?dateFrom=2018-01-01T00:00:00&dateTo=2018-12-01T00:00:00&sort=date_order&searchTerm=O-2018

( sort= date_order )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM