Storing a JSON schema in mongodb with spring

前端 未结 5 1116
刺人心
刺人心 2021-01-05 11:21

I am new to Spring data and mongodb. I have a JSON object which represents a JSON Schema and I need to store that in mongodb using spring data. But the issue with JSON schem

5条回答
  •  逝去的感伤
    2021-01-05 11:45

    You can map embedded documents using @DBref

    @Document(collection = "first")
    public class First {
    
        @Id
        private String id;
    
        @DBRef
        private Properties properties;
    
        @Field
        private List required;
    
        // constructor
        // getters and setter    
    }
    
    public class Properties {
    
        @Id
        private String id;
    
        @DBRef
        private Name name;
    
        @DBRef
        private Age age;
    
        // constructor
        // getters and setter   
    }
    
    public class Name { ... }
    public class Age { ... }
    

    http://www.baeldung.com/cascading-with-dbref-and-lifecycle-events-in-spring-data-mongodb

    http://docs.spring.io/spring-data/data-mongo/docs/1.4.2.RELEASE/reference/html/mapping-chapter.html#mapping-usage-references

    Or as Angelo Immediata suggested

    @Document(collection = "first")
    public class First {
    
        @Id
        private String id;
    
        @Field
        private Map properties;
    
        @Field
        private List required;
    
        // constructor
        // getters and setter    
    }
    

    And you will need some custom read and write converters

    http://docs.spring.io/spring-data/data-mongo/docs/1.4.2.RELEASE/reference/html/mapping-chapter.html#mapping-explicit-converters

提交回复
热议问题