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

        <tfoot id='1hW42'></tfoot>
      2. <small id='1hW42'></small><noframes id='1hW42'>

          <bdo id='1hW42'></bdo><ul id='1hW42'></ul>

      3. Spring Data Mongo - 如何映射继承的 POJO 实体?

        Spring Data Mongo - How to map inherited POJO entities?(Spring Data Mongo - 如何映射继承的 POJO 实体?)
            <i id='XB9CG'><tr id='XB9CG'><dt id='XB9CG'><q id='XB9CG'><span id='XB9CG'><b id='XB9CG'><form id='XB9CG'><ins id='XB9CG'></ins><ul id='XB9CG'></ul><sub id='XB9CG'></sub></form><legend id='XB9CG'></legend><bdo id='XB9CG'><pre id='XB9CG'><center id='XB9CG'></center></pre></bdo></b><th id='XB9CG'></th></span></q></dt></tr></i><div id='XB9CG'><tfoot id='XB9CG'></tfoot><dl id='XB9CG'><fieldset id='XB9CG'></fieldset></dl></div>

            • <bdo id='XB9CG'></bdo><ul id='XB9CG'></ul>

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

                <tbody id='XB9CG'></tbody>
                <tfoot id='XB9CG'></tfoot><legend id='XB9CG'><style id='XB9CG'><dir id='XB9CG'><q id='XB9CG'></q></dir></style></legend>
                • 本文介绍了Spring Data Mongo - 如何映射继承的 POJO 实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 Spring 还很陌生,但我想尝试一下这个项目.我有一个 MongoDB 数据库,其中填充了非常复杂的文档.我想使用 Spring data Mongo 来查询(没有其他 CRUD 操作)数据库.

                  我已经使用 POJO 描述了我的文档实体,但其中一些是抽象的(请参阅 GeometryGeoJSON 用于接受所有类型的 GeoJson 几何,或 Contact 可以是PersonOrganisation.下面提供了 GitHub 存储库的链接.

                  使用该实体定义进行测试,抛出一个 java.lang.InstantiationError,这是公平的,因为在这些抽象类中没有定义构造函数.

                  这里是

                  Contact 可以是 PersonOrganisation.

                  如果您使用的是 spring-data-mongodb MongoRepository 根据你的实体模型在你的数据库中写入数据,一个 _class 字段将被添加到文档根和复杂的属性类型(查看本节).此字段存储 Java 类的完全限定名称,并且在从 MongoDb 文档映射到 Spring 数据模型时允许消除歧义.

                  如果您的应用程序只是从数据库中读取文档(没有 _class 字段),您需要告诉 Spring 数据在映射 Contact 时要实例化哪个类.Spring-data 允许您使用 Converter 自定义默认类型映射行为.使用显式 Converter 覆盖类的默认映射.你需要明确地映射你的整个班级.这是我的 ContactReadConverter 的示例:

                  @ReadingConverter公共类ContactReadConverter实现Converter<Document, Contact>{@覆盖公共联系人转换(文档来源){if (source.get("firstName") == null) {组织组织=新组织();I18n 名称 = 新 I18n();name.setEn(source.get("name", Document.class).get("en", String.class));name.setFr(source.get("name", Document.class).get("fr", String.class));组织.setName(名称);organization.setAcronym(source.get("acronym", String.class));组织.setRole(source.get("角色", String.class));返回组织;}人人 = 新人();person.setFirstName(source.get("firstName", String.class));person.setLastName(source.get("lastName", String.class));person.setRole(source.get("role", String.class));person.setEmail(source.get("email", String.class));person.setOrcId(source.get("orcId", String.class));if (source.get("organisation") != null) {文档 sourceOrg = source.get("组织", Document.class);组织组织=新组织();organization.setAcronym(sourceOrg.get("acronym", String.class));组织.setRole(sourceOrg.get("角色", String.class));if (sourceOrg.get("name") != null) {I18n 名称 = 新 I18n();name.setFr(sourceOrg.get("name", Document.class).get("fr", String.class));name.setEn(sourceOrg.get("name", Document.class).get("en", String.class));组织.setName(名称);}person.setOrganisation(组织);}返回人;}}

                  然后,需要注册新定义的转换器:

                  @Configuration公共类 DataportalApplicationConfig 扩展 AbstractMongoConfiguration {@Value("${spring.data.mongodb.uri}")私有字符串 uri;@Value("${spring.data.mongodb.database}")私有字符串数据库;@覆盖公共 MongoClient mongoClient() {返回新的 MongoClient(new MongoClientURI(uri));}@覆盖受保护的字符串 getDatabaseName() {返回数据库;}@豆角,扁豆@覆盖公共 MongoCustomConversions customConversions() {列表<转换器<?, ?>>converterList = new ArrayList<>();converterList.add(new ContactReadConverter());返回新的 MongoCustomConversions(converterList);}}

                  希望对你有帮助.

                  I'm fairly new to Spring but I want to give it a try on this project. I have a MongoDB database populated with quite complexe documents. I want to use Spring data Mongo to query (no other CRUD operations) the database.

                  I already described my document entity using POJOs but some of them are abstract (see GeometryGeoJSON is used to accept all type of GeoJson geometry, or Contact that can be a Person or an Organisation. The link to the GitHub repo is provided below).

                  Having a test with that entity definition, a java.lang.InstantiationError is thrown which fair since no Contructor are defined in those abstract classes.

                  Here is GitHub repository in case you need to have a look.

                  I feel a bit lost with all this but I'll have a more careful look at the documentation.

                  How would you face this issue ?

                  解决方案

                  I'll answer my own question. As mentioned in the comments, the solution is to use Converter.

                  Here is an example of what I intended to achieve with my class model :

                  A Contact can be either a Person or an Organisation.

                  If you are using spring-data-mongodb MongoRepository to write data in your database according to your entity model, a _class field will be added to document roots and to complex property types (see this section). This fields store the fully qualified name of the Java class and it allows disambiguation when mapping from MongoDb Document to Spring data model.

                  If your app just read document from the database (no _class fields), you need to tell Spring data which class to instantiate when mapping a Contact. Spring-data allows you to customize default type mapping behaviour using Converter. Using explicit Converter override default mapping for the class. you need to explicitly map your entire class. Here is an example of my ContactReadConverter:

                  @ReadingConverter
                  public class ContactReadConverter implements Converter<Document, Contact> {
                  
                      @Override
                      public Contact convert(Document source) {
                          if (source.get("firstName") == null) {
                              Organisation organisation = new Organisation();
                              I18n name = new I18n();
                              name.setEn(source.get("name", Document.class).get("en", String.class));
                              name.setFr(source.get("name", Document.class).get("fr", String.class));
                              organisation.setName(name);
                              organisation.setAcronym(source.get("acronym", String.class));
                              organisation.setRole(source.get("role", String.class));
                              return organisation;
                          }
                          Person person = new Person();
                          person.setFirstName(source.get("firstName", String.class));
                          person.setLastName(source.get("lastName", String.class));
                          person.setRole(source.get("role", String.class));
                          person.setEmail(source.get("email", String.class));
                          person.setOrcId(source.get("orcId", String.class));
                          if (source.get("organisation") != null) {
                              Document sourceOrg = source.get("organisation", Document.class);
                              Organisation organisation = new Organisation();
                              organisation.setAcronym(sourceOrg.get("acronym", String.class));
                              organisation.setRole(sourceOrg.get("role", String.class));
                              if (sourceOrg.get("name") != null) {
                                  I18n name = new I18n();
                                  name.setFr(sourceOrg.get("name", Document.class).get("fr", String.class));
                                  name.setEn(sourceOrg.get("name", Document.class).get("en", String.class));
                                  organisation.setName(name);
                              }
                              person.setOrganisation(organisation);
                          }
                          return person;
                      }
                  }
                  

                  Then, newly defined converters need to be registered:

                  @Configuration
                  public class DataportalApplicationConfig extends AbstractMongoConfiguration {
                      @Value("${spring.data.mongodb.uri}")
                      private String uri;
                      @Value("${spring.data.mongodb.database}")
                      private String database;
                      @Override
                      public MongoClient mongoClient() {
                          return new MongoClient(new MongoClientURI(uri));
                      }
                      @Override
                      protected String getDatabaseName() {
                          return database;
                      }    
                      @Bean
                      @Override
                      public MongoCustomConversions customConversions() {
                          List<Converter<?, ?>> converterList = new ArrayList<>();
                          converterList.add(new ContactReadConverter());
                          return new MongoCustomConversions(converterList);
                      }
                  }
                  

                  Hope it helps.

                  这篇关于Spring Data Mongo - 如何映射继承的 POJO 实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                        • <small id='JiYax'></small><noframes id='JiYax'>

                            <bdo id='JiYax'></bdo><ul id='JiYax'></ul>

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