Search code examples
springmongodbspring-bootspring-dataembedded-documents

MongoDB One-to-Many and Many-to-One relations in Spring Boot Project


please how can I add relations in a mongoDB cause I just start using it. For example Comment id is a foreign key:

 @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
    }

Solution

  • The equivalence of the JPA annotation @OneToMany and @ManyToMany is @DBRef