• <bdo id='5THpq'></bdo><ul id='5THpq'></ul>
      <legend id='5THpq'><style id='5THpq'><dir id='5THpq'><q id='5THpq'></q></dir></style></legend>

    1. <i id='5THpq'><tr id='5THpq'><dt id='5THpq'><q id='5THpq'><span id='5THpq'><b id='5THpq'><form id='5THpq'><ins id='5THpq'></ins><ul id='5THpq'></ul><sub id='5THpq'></sub></form><legend id='5THpq'></legend><bdo id='5THpq'><pre id='5THpq'><center id='5THpq'></center></pre></bdo></b><th id='5THpq'></th></span></q></dt></tr></i><div id='5THpq'><tfoot id='5THpq'></tfoot><dl id='5THpq'><fieldset id='5THpq'></fieldset></dl></div>
    2. <tfoot id='5THpq'></tfoot>

      <small id='5THpq'></small><noframes id='5THpq'>

    3. 如何将具有动态键的文档映射到 Spring MongoDb 实体类

      时间:2024-08-23
    4. <legend id='G5r1v'><style id='G5r1v'><dir id='G5r1v'><q id='G5r1v'></q></dir></style></legend>
    5. <tfoot id='G5r1v'></tfoot>
    6. <i id='G5r1v'><tr id='G5r1v'><dt id='G5r1v'><q id='G5r1v'><span id='G5r1v'><b id='G5r1v'><form id='G5r1v'><ins id='G5r1v'></ins><ul id='G5r1v'></ul><sub id='G5r1v'></sub></form><legend id='G5r1v'></legend><bdo id='G5r1v'><pre id='G5r1v'><center id='G5r1v'></center></pre></bdo></b><th id='G5r1v'></th></span></q></dt></tr></i><div id='G5r1v'><tfoot id='G5r1v'></tfoot><dl id='G5r1v'><fieldset id='G5r1v'></fieldset></dl></div>

              <bdo id='G5r1v'></bdo><ul id='G5r1v'></ul>
                <tbody id='G5r1v'></tbody>

                <small id='G5r1v'></small><noframes id='G5r1v'>

                本文介绍了如何将具有动态键的文档映射到 Spring MongoDb 实体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个可以有动态键名的文档:

                I have a document that can have dynamic key names:

                {
                "_id" : ObjectId("51a29f6413dc992c24e0283e"),
                "envinfo" : {
                    "appName" : "MyJavaApp",
                    "environment" : {
                        "cpuCount" : 12,
                        "heapMaxBytes" : 5724766208,
                        "osVersion" : "6.2",
                        "arch" : "amd64",
                        "javaVendor" : "Sun Microsystems Inc.",
                        "pid" : 44996,
                        "javaVersion" : "1.6.0_38",
                        "heapInitialBytes" : 402507520,
                }
                

                这里 envinfo 的键是事先不知道的.在 Spring Data Mongodb 中创建将映射此文档的实体类的最佳方法是什么?

                Here envinfo 's keys are not known in advance. What is the best way to create an entity class in Spring Data Mongodb which will map this document?

                推荐答案

                这就是我要做的.

                class EnvDocuemnt {
                
                    @Id
                    private String id; //getter and setter omitted
                
                    @Field(value = "envinfo")
                    private BasicDBObject infos;
                
                    public Map getInfos() {
                        // some documents don't have any infos, in this case return null...
                        if ( null!= infos)
                            return infos.toMap();
                        return null;
                    }
                
                    public void setInfos(Map infos) {
                        this.infos = new BasicDBObject( infos );
                    }
                
                }
                

                这样,getInfos() 返回一个 Map,您可以在需要时使用 String 键进行探索,并且可以嵌套 Map.

                This way, getInfos() returns a Map<String,Object> you can explore with String keys when needed, and that can have nested Map.

                对于你的依赖,最好不要直接暴露 BasicDBObject 字段,这样可以在不包含任何 MongoDb 库的代码中通过接口使用.

                For your dependencies, it is better not to expose the BasicDBObject field directly, so this can be used via interface in a code not including any MongoDb library.

                注意,如果envinfo中有一些经常访问的字段,那么最好将它们声明为你的类中的字段,有一个直接访问器,这样就不用花太多时间一遍又一遍地浏览地图.

                Note that if there is some frequent accessed fields in envinfo, then it would be better to declare them as fields in your class, to have a direct accessor, and so not to spend to much time in browsing the map again and again.

                这篇关于如何将具有动态键的文档映射到 Spring MongoDb 实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:PersistenceConstructor 参数变量名称与实例变量名称不匹配 下一篇:春季数据中的@Transient 不起作用

                相关文章

                <legend id='Cv9QK'><style id='Cv9QK'><dir id='Cv9QK'><q id='Cv9QK'></q></dir></style></legend>
                1. <i id='Cv9QK'><tr id='Cv9QK'><dt id='Cv9QK'><q id='Cv9QK'><span id='Cv9QK'><b id='Cv9QK'><form id='Cv9QK'><ins id='Cv9QK'></ins><ul id='Cv9QK'></ul><sub id='Cv9QK'></sub></form><legend id='Cv9QK'></legend><bdo id='Cv9QK'><pre id='Cv9QK'><center id='Cv9QK'></center></pre></bdo></b><th id='Cv9QK'></th></span></q></dt></tr></i><div id='Cv9QK'><tfoot id='Cv9QK'></tfoot><dl id='Cv9QK'><fieldset id='Cv9QK'></fieldset></dl></div>

                2. <tfoot id='Cv9QK'></tfoot>
                    <bdo id='Cv9QK'></bdo><ul id='Cv9QK'></ul>

                3. <small id='Cv9QK'></small><noframes id='Cv9QK'>