我将 SpringBoot REST API 微服务与 MongoDB 一起使用。我有球员、比赛和得分三种服务。对于每个服务,我都有一个 MongoDB 数据库。在 Score 服务的 POST 方法中,我检查 plyare 和 game 是否存在,然后对于存在的 player 和 game,创建一个新分数。为此,起初我使用 player-repository 和 game-repository 来检查它们是否存在,但之后我阅读了一些关于 Relashionships(OneToMany, ManyToOne) 和关于 Refrence 的文档,@DBRef,我尝试使用它们。问题是例如在关系中,我感兴趣的是只有变量昵称是玩家类中的 ID,而变量代码是游戏类中的 ID 而不是其他变量,而是在分数类中,当我定义
private Player player;
我有一个 Player 类的对象及其所有成员变量,游戏也是如此。我对这个分数表感兴趣:
## player
{
“nickname”: “pippo”,
“firstname”: “Filippo”,
“lastname”: “Rossi”,
“email”: “filipporox@mail.com”
}
## game
{
“code”: “XFR453”,
“title”: “The Game”,
“softwarehouse”: “SWGameS”,
“version”: “1”,
“releaseyear”: “2020”
}
***## score
{
“player”: “pippo”,
“game”: “XFR453”,
“score”: “232209”,
“date”: “2020/04/28”,
“history”: [
{ “score”: “232209”, “date”: “2020/04/28” },
{ “score”: “220011”, “date”: “2020/04/23” },
{ “score”: “182210”, “date”: “2020/03/12” }
]
}***
你能帮我处理上面的例子吗?分数等级:
@Document(collection = "score")
public class Score {
@Id
@NotBlank
@JsonView(Views.class)
private String scoreid;
@JsonView(Views.class)
private Long score;
@JsonView(Views.class)
private Date date;
@NotBlank
@JsonView(Views.class)
private Player player;
@NotBlank
@JsonView(Views.class)
private Game game;
public List<String> history;
public String getScoreid() {
return scoreid;
}
public void setScoreid(String scoreid) {
this.scoreid = scoreid;
}
public Long getScore() {
return score;
}
public void setScore(Long score) {
this.score = score;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public Game getGame() {
return game;
}
public void setGame(Game game) {
this.game = game;
}
public List<String> getHistory() {
return history;
}
public void setHistory(List<String> history) {
this.history = history;
}
}
对不起,我的英语不好。