Spring Data Rest-按嵌套属性排序 [英] Spring Data Rest - sort by nested property

查看:186
本文介绍了Spring Data Rest-按嵌套属性排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Spring Boot 1.5.1和Spring Data Rest的数据库服务.我将实体存储在MySQL数据库中,并使用Spring的PagingAndSortingRepository通过REST访问它们.我发现指出支持按嵌套参数排序,但是我找不到按嵌套字段排序的方法.

I have a database service using Spring Boot 1.5.1 and Spring Data Rest. I am storing my entities in a MySQL database, and accessing them over REST using Spring's PagingAndSortingRepository. I found this which states that sorting by nested parameters is supported, but I cannot find a way to sort by nested fields.

我有这些课程:

@Entity(name = "Person")
@Table(name = "PERSON")
public class Person {
    @ManyToOne
    protected Address address;

    @ManyToOne(targetEntity = Name.class, cascade = {
        CascadeType.ALL
    })
    @JoinColumn(name = "NAME_PERSON_ID")
    protected Name name;

    @Id
    protected Long id;

    // Setter, getters, etc.
}

@Entity(name = "Name")
@Table(name = "NAME")
public class Name{

    protected String firstName;

    protected String lastName;

    @Id
    protected Long id;

    // Setter, getters, etc.
}

例如,使用方法时:

Page<Person> findByAddress_Id(@Param("id") String id, Pageable pageable);

并调用URI http://localhost :8080/people/search/findByAddress_Id?id = 1& sort = name_lastName,desc ,Spring会完全忽略sort参数.

And calling the URI http://localhost:8080/people/search/findByAddress_Id?id=1&sort=name_lastName,desc, the sort parameter is completely ignored by Spring.

参数 sort = name.lastName sort = nameLastName 也不起作用.

我形成Rest请求的方式是否错误,或者缺少某些配置?

Am I forming the Rest request wrong, or missing some configuration?

谢谢!

推荐答案

我对此进行了调试,看起来像艾伦(Alan)提到的问题.

I debugged through that and it looks like the issue that Alan mentioned.

我发现了可以解决问题的解决方法:

I found workaround that could help:

创建自己的控制器,注入您的仓库和可选的投影工厂(如果需要投影).实现get方法以将调用委派给您的存储库

Create own controller, inject your repo and optionally projection factory (if you need projections). Implement get method to delegate call to your repository

 @RestController
 @RequestMapping("/people")
 public class PeopleController {

    @Autowired
    PersonRepository repository;

    //@Autowired
    //PagedResourcesAssembler<MyDTO> resourceAssembler;

    @GetMapping("/by-address/{addressId}")
    public Page<Person> getByAddress(@PathVariable("addressId") Long addressId, Pageable page)  {

        // spring doesn't spoil your sort here ... 
        Page<Person> page = repository.findByAddress_Id(addressId, page)

        // optionally, apply projection
        //  to return DTO/specifically loaded Entity objects ...
        //  return type would be then PagedResources<Resource<MyDTO>>
        // return resourceAssembler.toResource(page.map(...))

        return page;
    }

}

这对我来说适用于2.6.8.RELEASE;这个问题似乎存在于所有版本中.

This works for me with 2.6.8.RELEASE; the issue seems to be in all versions.

这篇关于Spring Data Rest-按嵌套属性排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆