简体   繁体   English

如何使用DozerBeanMapper将HashMap与数组对象列表(列表是哈希映射中的值)映射到另一个HashMap?

[英]How to map HashMap with Array List of Objects (list is the value in the hash map) into another HashMap using DozerBeanMapper?

I have two classes and want to map the properties from Female object to Male object using org.dozer.Mapper( http://dozer.sourceforge.net/ ). 我有两个类,并希望使用org.dozer.Mapper( http://dozer.sourceforge.net/ )将Female对象的属性映射到Male对象。

The first class is: 第一堂课是:

public class Male {
    private String name;
    private String surname;

    private Map<String, List<Contact>> contacts;
....

and the second class is : 第二类是:

public class Female {
    private String name;
    private String surname;
    private String mobile;
    private String dateOfBirth;

    private Map<String, List<Contact>> contacts;
...

and the third class is : 第三类是:

public class Contact {
    private String street;
    private String postcode;
    private String email;
...

The Map that i am using like object property is LinkedHashMap and the List which is a value in the Map is ArrayList. 我正在使用的Map就像对象属性一样是LinkedHashMap而List中的值是MapList。 When I try to map them using the dozer, the array list which is the value in the hash map is not a list with objects and looks like in the picture: 当我尝试使用推土机映射它们时,作为散列映射中的值的数组列表不是带有对象的列表,如图所示:

        Map<String, List<Contact>> contact = new LinkedHashMap<>();
        List<Contact> listOfContacts = new ArrayList<>();
        Contact contactObj = new Contact();
        contactObj.setEmail("lala@gmail.com");
        contactObj.setPostcode("1233355");
        contactObj.setStreet("street");

        listOfContacts.add(contactObj);

        contact.put("2131323213", listOfContacts);
        femaleObj.setContact(contact);

        Mapper objectMapper = new DozerBeanMapper();
        Male maleObj = objectMapper.map(femaleObj, Male.class);

在此输入图像描述

How can I get the list of objects in the List in the Male object? 如何在Male对象的List中获取对象列表?

At first, I've tried your code as-is and I got the same behavior. 起初,我已经按原样尝试了你的代码,我也有同样的行为。

Then, I've explicit set the mapping configuration with b-hint (see documentation about this ) as follow and I got what you need. 然后,我明确地用b-hint设置映射配置(参见相关文档 ),如下所示,我得到了你需要的东西。

First case - Java Configuration (create a class that extends BeanMappingBuilder): 第一种情况 - Java Configuration (创建一个扩展BeanMappingBuilder的类):

public class CustomMapper extends BeanMappingBuilder {
    @Override
    protected void configure() {
        mapping(Female.class, Male.class).fields("contacts", "contacts", FieldsMappingOptions.hintB(Contact.class));
    }
}

Second case - XML Configuration : 第二种情况 - XML配置

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">

  <configuration>
    <stop-on-errors>true</stop-on-errors>
    <wildcard>true</wildcard>
  </configuration>

  <mapping>
    <class-a>blog.valerioemanuele.dozer.Female</class-a>
    <class-b>blog.valerioemanuele.dozer.Male</class-b>

      <field>
        <a>contacts</a>
        <b>contacts</b>
        <b-hint>blog.valerioemanuele.dozer.Contact</b-hint> 
      </field>
  </mapping> 


</mappings>

Here the unit tests I've executed: 这里我执行的单元测试:

import org.dozer.DozerBeanMapper;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class TestFemaleToMaleConversion {

    private static Female femaleObj;

    @BeforeAll
    private static void initData() {
        Map<String, List<Contact>> contact = new LinkedHashMap<>();
        List<Contact> listOfContacts = new ArrayList<>();
        Contact contactObj = new Contact();
        contactObj.setEmail("lala@gmail.com");
        contactObj.setPostcode("1233355");
        contactObj.setStreet("street");

        listOfContacts.add(contactObj);

        contact.put("2131323213", listOfContacts);
        femaleObj = new Female();
        femaleObj.setName("Elisabeth");
        femaleObj.setSurname("Chesny");
        femaleObj.setContacts(contact);
    }

    @Test
    void testWithXmlMapping() {
        DozerBeanMapper objectMapper = new DozerBeanMapper();
        objectMapper.setMappingFiles(Arrays.asList("dozer-mapping.xml"));
        Male maleObj = objectMapper.map(femaleObj, Male.class);

        Assert.assertEquals("lala@gmail.com", maleObj.getContacts().get("2131323213").get(0).getEmail());
    }

    @Test
    void testWithJavaMapping() {
        DozerBeanMapper objectMapper = new DozerBeanMapper();
        objectMapper.addMapping(new CustomMapper());
        Male maleObj = objectMapper.map(femaleObj, Male.class);

        Assert.assertEquals("street", maleObj.getContacts().get("2131323213").get(0).getStreet());
    }
}

Here the result: 结果如下:


在此输入图像描述

You can get the complete code from my GitHub repository . 您可以从我的GitHub存储库中获取完整的代码。 The example was developed with Java8, Maven and Junit5. 该示例是使用Java8,Maven和Junit5开发的。

EDIT: I've added Java mapping configuration case. 编辑:我添加了Java映射配置案例。 Taking inspiration from another post 另一篇文章中获取灵感

If you wanna achieve the same with JAVA code instead XML config use this: 如果你想用JAVA代码实现相同而不是XML配置使用这个:

public class DemoProvider extends BeanMappingBuilder {

    @Override
    protected void configure() {
        mapping(Female.class, Male.class,
                TypeMappingOptions.oneWay()
        )
                .fields("contact", "contact",
                        FieldsMappingOptions.collectionStrategy(true, RelationshipType.NON_CUMULATIVE),
                        FieldsMappingOptions.hintA(Contact.class),
                        FieldsMappingOptions.hintB(Contact.class),
                        FieldsMappingOptions.oneWay()
                );
      }

}

usage: 用法:

DemoProvider demoProvider = new DemoProvider();
DozerBeanMapper objectMapper = new DozerBeanMapper();
objectMapper.addMapping(demoProvider);
Male maleObj = objectMapper.map(femaleObj, Male.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Map或HashMap,列表还是ArrayList? - Map or HashMap, List or ArrayList? 如何迭代哈希图列表并将哈希图映射到模型 - How to iterate list of hashmaps and map hashmap to model List的Java映射值到Hashmap中 - Java map values of List into a Hashmap 为什么 clear hashmap 方法会清除数组列表中添加的映射 - Why does clear hashmap method clears added map in array list 将元素加入列表并将列表映射到哈希图 - Join element into a list and map the list to hashmap 如何在单个列表中添加多个值HashMap <Integer, ArrayList<User> &gt; map1 =新的HashMap <Integer, ArrayList<User> &gt;(); - How to add a multiple value in a single list HashMap<Integer, ArrayList<User>> map1 = new HashMap<Integer, ArrayList<User>>(); 如何根据 class 和 Map 到 HashMap 的属性将对象收集到 Z93F725A07423FE1C8464D? - How to collect objects to List based on properties of the class and then Map to HashMap in java 8 or 11? 我如何从地图中获取价值 <Object,List<Object> &gt; obj = new HashMap <Object,List<Object> &gt;(); - How can i retrieve value from Map<Object,List<Object>> obj=new HashMap<Object,List<Object>>(); 使用Java将地图数据分组到嵌套HashMap中 - Group List of Map Data into Nested HashMap in Java Java和数据结构(Map,Hashmap,List,Arraylist等) - Java and data structures (Map, Hashmap, List, Arraylist, …)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM