Mongo 使用 Spring 数据实现数据库实体关系

Mongo db entity relationship implementation using Spring data

我正在使用 Mongo DB 学习 Spring,我在学习 entity-relationship 模型时感到困难。 谁能教我如何实现以下设计?

人collection 一个class

  • 编号
  • 名字
  • 运动列表object

运动 collection 一项运动 class

  • id (Auto-generated)
  • 运动名称

当我保存包含运动 class 的人 class 时,Sports 实体应该保存在 Sports collection 中,如果它还不存在,Person 实体应该存储在人 collection 有运动 object 参考。

当我检索人物 class 时,应从相应的 collection 中检索相关运动 class。 我试过 @DBRef 但它对我不起作用。 如果有人教我这个场景或提供学习这个概念的参考,那将是非常有帮助的。

非常感谢。

while I am saving the person class which contains sports class, Sports entity should be saved in Sports collection if it is not already present and Person entity should be stored in Person collection with Sports objects Reference.

Spring-data-mongo 中不支持级联保存。因此引用对象不会自动保存到数据库中。要达到同样的效果,您有两种选择。
1)首先,保存运动集合(如果在集合中找不到该记录)然后保存运动对人物集合的引用。
2)让你自定义级联保存实现。有关参考,请参阅 this