MongoDB에는 고객 정의 레이블을 포함하는 Customer라는 문서 모음이 있습니다. 내 도메인 객체 (롬복 주석 포함)과 같이 :스프링 데이터 REST에 임베디드 문서의 링크 관계에 대한 내 욕구를 어떻게 알릴 수 있습니까?
@Document(collection = "Customer")
@Getter
@Setter
public class Customer {
@Id
long id;
@Field("name")
String name;
@Field("labels")
List<CustomerLabel> labels;
}
@Getter
@Setter
public class CustomerLabel {
@Id
@Field("label_id")
long labelId;
@Field("label_name")
String labelName;
}
지금 GET /customers
에 대한 응답은 다음과 같습니다
{
"_links": {
"self": {
"href": "http://localhost:8080/app/customers?page=&size=&sort="
}
},
"_embedded": {
"customers": [
{
"name": "Smith, Jones, and White",
"labels": [
{
"labelName": "General label for Smith, Jones, and White"
}
],
"_links": {
"self": {
"href": "http://localhost:8080/app/customers/285001"
}
}
}
]
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
내가 포함 된 라벨 문서로 "불러"하고 싶은
{
"_links": {
"self": {
"href": "http://localhost:8080/app/customers?page=&size=&sort="
}
},
"_embedded": {
"customers": [
{
"name": "Smith, Jones, and White",
"_links": [
{
"labels": {
"href": "http://localhost:8080/app/customers/285001/labels"
},
{
"self": {
"href": "http://localhost:8080/app/customers/285001"
}
}]
}
]
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
가 어떻게 내 응용 프로그램에서이 작업을 수행 할 수 있습니다 별도의 연결 관계는, 그래서
GET /customers
에 대한 응답은 다음과 같이 더 본다?
당신의 제안은 데이터베이스 구조에 링크 관계를 더 밀접하게 연결하는 것입니다. 나는 그것을 잘못 읽고 있습니까? 그것은 차선 설계의 사례가 아닐까요? –
아니요, 링크 관계의 수준이 아닙니다. 나는 class-to-document-mapping이 최선의 방법보다 차선책이라고 주장한다. 임베디드 문서 저장소를 갖는 것은 집계 디자인 원칙을 완전히 위반하는 것입니다. 어떤 종류의 웹 서비스를 구축하고 싶은지는 완전히 직관적 인 질문입니다. 그러나 근본적으로 잘못 설계된 무언가의 바로 위에 무언가를하려고하면 어떤 방법으로도 작동하지 않습니다. :) –
죄송합니다. 이해하려고합니다. 임베디드 문서 자체가 차선 설계라고 주장하거나, 저장소가 아닌 다른 것을 반환하는 것이 차선책 일 것이라고 말하고 있습니까? –