Sort function at join endpoint in Spring Data Rest

I have the following two resources and their relationship:

@Table(name = "Item")
@Data
@Entity
public class Item {

    @ManyToOne
    @JoinColumn(name = "fk_wrapper")
    private Wrapper wrapper;

    @Id
    @GeneratedValue
    private String id;

    private Integer someValue;
}

      

and

@Table(name = "Wrapper")
@Data
@Entity
public class Wrapper {

    @Id
    @GeneratedValue
    private String id;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "fk_wrapper")
    private List<Item> items;

    private String someField;
}

      

Then, first, I create Wrapper

;

POST http://localhost:8080/wrappers/
{
    "someField": "asd"
}

      

http://localhost:8080/wrappers/1

then i create two Item

's related to this Wrapper

;

POST http://localhost:8080/items/
{
    "someValue": "5",
    "wrapper": "http://localhost:8080/wrappers/1"
}

      

&

POST http://localhost:8080/items/
{
    "someValue": "7",
    "wrapper": "http://localhost:8080/wrappers/1"
}

      

After that, when I call the endpoint http://localhost:8080/wrappers/1/items

, I get a list of these two items as expected, but what is the problem, I cannot seem to have a sort function on that endpoint. I seem to be able to sort at the endpoint http://localhost:8080/items

, but getting the association link there seems to be no sorting function. Is it a lack of sorting, or am I missing some configuration?


PS when I create my own search method like:

@RepositoryRestResource
public interface ItemRepository extends JpaRepository<Item, String> {
    List<Item> findByWrapper_Id(@Param("id") String id, Sort sort);
}

      

I can then use an endpoint sort http://localhost:8080/items/search/findByWrapper_Id

, but too ugly imo considering there is already an autogenerated endpoint.

+3


source to share


1 answer


Spring Data Rest does not support sorting by associations.

You seem to have already found the best way to do what you need according to the Spring Data Rest command - create a query to get the data you need. This will actually support both pagination and sorting.

The reason it is not supported has to do with when requests are made to fetch the underlying resource (before the association endpoints are created) and the fact that the connection endpoint is using associated object associations directly and that a new one would have to be created anyway to support sorting. request.



More detailed information can be found here:

https://jira.spring.io/browse/DATAREST-725?focusedCommentId=122244&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-122244

Hooray!

0


source







All Articles