[英]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.