[SPRING] Spring 데이터 나머지 - 중첩 된 속성별로 정렬
SPRINGSpring 데이터 나머지 - 중첩 된 속성별로 정렬
스프링 부트 1.5.1 및 스프링 데이터 나머지를 사용하는 데이터베이스 서비스가 있습니다. 엔티티를 MySQL 데이터베이스에 저장하고 Spring의 PagingAndSortingRepository를 사용하여 REST를 통해 액세스합니다. 중첩 된 매개 변수를 기준으로 정렬이 지원된다는 것을 알게되었지만 중첩 된 필드별로 정렬하는 방법을 찾을 수 없습니다.
나는이 수업을 가지고 :
@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를 호출하면 sort 매개 변수는 Spring에 의해 완전히 무시됩니다.
sort = name.lastName 및 sort = nameLastName 매개 변수도 작동하지 않았습니다.
휴식 요청이 잘못 구성되었거나 일부 구성이 누락 되었습니까?
고맙습니다!
해결법
-
==============================
1.나는 그것을 통해 디버깅을했고 Alan이 언급 한 문제와 비슷하게 보입니다.
나는 그것을 통해 디버깅을했고 Alan이 언급 한 문제와 비슷하게 보입니다.
내가 도울 수있는 해결 방법을 찾았습니다 :
자체 컨트롤러를 만들고, repo 및 선택적으로 프로젝션 팩토리를 주입하십시오 (투영이 필요한 경우). 저장소에 호출을 위임하기 위해 get 메소드 구현
@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; 문제는 모든 버전에있는 것으로 보입니다.
-
==============================
2.내가 찾은 해결 방법은 정렬 목적으로 만 추가 읽기 전용 속성을 만드는 것입니다. 위의 예제를 기반으로 작성하십시오.
내가 찾은 해결 방법은 정렬 목적으로 만 추가 읽기 전용 속성을 만드는 것입니다. 위의 예제를 기반으로 작성하십시오.
@Entity(name = "Person") @Table(name = "PERSON") public class Person { // read only, for sorting purposes only // @JsonIgnore // we can hide it from the clients, if needed @RestResource(exported=false) // read only so we can map 2 fields to the same database column @ManyToOne @JoinColumn(name = "address_id", insertable = false, updatable = false) private Address address; // We still want the linkable association created to work as before so we manually override the relation and path @RestResource(exported=true, rel="address", path="address") @ManyToOne private Address addressLink; ... }
제안 된 해결 방법의 단점은 이제 중첩 된 정렬을 지원하려는 모든 속성을 명시 적으로 복제해야한다는 것입니다.
나중에 편집 : 또 다른 단점은 우리가 클라이언트에서 내장 된 속성을 숨길 수 없다는 것입니다. 내 원래의 대답에서, 나는 우리가 @JsonIgnore를 추가 할 수 있다고 제안하고 있었지만 분명히 그 종류를 깨뜨렸다.
from https://stackoverflow.com/questions/42262846/spring-data-rest-sort-by-nested-property by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] Spring DATA JPA로 커스텀 질의 생성하기? (0) | 2019.04.17 |
---|---|
[SPRING] Spring 구성 요소 스캔의 성능은 스캔 한 패키지의 크기에 좌우됩니까? (0) | 2019.04.17 |
[SPRING] Hibernate는 폐 대신 BigInteger를 리턴한다. (0) | 2019.04.17 |
[SPRING] Spring RabbitMQ로 새로운 큐를 생성하는 방법? (0) | 2019.04.17 |
[SPRING] db에서 cron 표현식을로드하고 Spring에서 다시 스케줄하는 방법은 무엇입니까? (0) | 2019.04.17 |