MongoDBデータベースを使用した非常に簡単なプロジェクトでSpringデータを学習し始めました.DBRefを使用する際にいくつか問題があります。DBRefオブジェクトでのSpring Data Projectionの使用
説明
オーガナイザー(CD)と1対多の参加者との単純な競争を整理する必要があります私のプロジェクト。人々は複数の競技に参加できるので、私は競技会と人物の両方のためのレポジトリを作った。
完全なコードはGitHubの上で見ることができます。
public class Competition {
@Id private String id;
private String name;
@DBRef
private Person organizer;
private List<Participant> participants = new ArrayList<>();
}
public class Participant {
private String freq;
@DBRef
private Person person;
}
public class Person {
@Id
private String id;
private String name;
}
リポジトリ:
@RepositoryRestResource(collectionResourceRel = "competition", path = "competition")
public interface CompetitionRepository extends MongoRepository<Competition, String> {
}
@RepositoryRestResource(collectionResourceRel = "person", path = "person")
public interface PersonRepository extends MongoRepository<Person, String> {
}
問題
私は要求しています。ここhttps://github.com/elkjaerit/rest-sample
は、基底クラスであります競争のリソース私はpの十分な情報を得ていない参加者 - 「freq」フィールドのみが表示されます。私は@Projectionを使ってみましたが、それをオーガナイザのために働かせることができましたが、参加者のpersonオブジェクトを取得する方法はわかりません。投影なし
結果
{
"_links": {
"competition": {
"href": "http://localhost:8080/competition/5710b32b03641c32671f885a{?projection}",
"templated": true
},
"organizer": {
"href": "http://localhost:8080/competition/5710b32b03641c32671f885a/organizer"
},
"self": {
"href": "http://localhost:8080/competition/5710b32b03641c32671f885a"
}
},
"name": "Competition #1",
"participants": [
{
"freq": "F0"
},
{
"freq": "F1"
},
{
"freq": "F2"
},
{
"freq": "F3"
}
]
}
と投影と
{
"_links": {
"competition": {
"href": "http://localhost:8080/competition/5710b32b03641c32671f885a{?projection}",
"templated": true
},
"organizer": {
"href": "http://localhost:8080/competition/5710b32b03641c32671f885a/organizer"
},
"self": {
"href": "http://localhost:8080/competition/5710b32b03641c32671f885a"
}
},
"name": "Competition #1",
"organizer": {
"name": "Competition organizer"
},
"participants": [
{
"freq": "F0"
},
{
"freq": "F1"
},
{
"freq": "F2"
},
{
"freq": "F3"
}
]
}
任意の提案ですか?