Spring Boot项目中的MongoDB一对多和多对一关系

Posted

技术标签:

【中文标题】Spring Boot项目中的MongoDB一对多和多对一关系【英文标题】:MongoDB One-to-Many and Many-to-One relations in Spring Boot Project 【发布时间】:2020-07-04 10:01:08 【问题描述】:

请问如何在 mongoDB 中添加关系,因为我刚开始使用它。例如评论id是外键

 @Document
    class Comment 

        @Id
        private String id;
        private String text;
        private String author;

        // constructors, getters and setters are ommited
    

    @Document
    class Article 
        @Id
        private String id;
        @DBRef(lazy = true)
        @CascadeSave
        private List<Comment> comments;
        private String title;
        private String text;

        // constructors, getters and setters are ommited
    

【问题讨论】:

【参考方案1】:

JPA注解@OneToMany和@ManyToMany的等价物是@DBRef

【讨论】:

以上是关于Spring Boot项目中的MongoDB一对多和多对一关系的主要内容,如果未能解决你的问题,请参考以下文章

基于spring boot的项目中的spring data mongodb配置

spring-boot整合mongodb多数据源的案例

spring boot + MyBatisPlus 一对多、多对一、多对多的解决方案

spring boot mybatis 一对一 一对多

Spring boot整合Mongodb

如何从 Spring Boot 项目中的子子表中检索数据?