1
我开始在使用MongoDB数据库的一个非常简单的项目上学习Spring Data,并且在使用DBRef时遇到了一些麻烦 - 并且可能在如何建模NoSQL db在DBRef对象上使用Spring Data Projection
说明
我的项目应组织与组织者(CD)和一个一对多参加一个简单的竞争。因为人们可以参加多项比赛,所以我为比赛和人员制作了资料库。
完整的代码可以看出在GitHub上:https://github.com/elkjaerit/rest-sample
这是基础类:
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> {
}
问题
当我请求竞争资源我没有得到足够的信息参与者 - 只显示“freq”字段。我试过使用@Projection并设法让它为组织者工作,但我不知道如何获取参与者的人员对象?
结果没有投影
{
"_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"
}
]
}
有什么建议?