How do I map a HashMap to a Pojo
I'm having difficulties mapping a HashMap to a Pojo using dozer. Note: the attribute names in my Pojo don't map to the key values in HashMap i've populated. I'm trying to use the set-method attribute in the dozer mapping file to map the hash key to the Pojo setter. When I run the code below, I don't get any exceptions, but the the Pojo isn't populated with any data from the hash. Any suggestions or feedback would be appreciated.
Pojo:
public class Hotel {
private String companyAssignedId;
public Hotel(){}
public String getCompanyAssignedId() {
return companyAssignedId;
}
public void setCompanyAssignedId(String companyAssignedId) {
this.companyAssignedId = companyAssignedId;
}
}
Mapping XML:
<mapping>
<class-a>com.reardencommerce.platformsvc.hadoop.dto.Hotel</class-a>
<class-b>java.util.Map</class-b>
<field>
<a set-method="setCompanyAssignedId">hotel</a&g开发者_JS百科t;
<b key="COMPANY_ASSG_ID">rawData</b>
</field>
</mapping>
Execution Code:
DozerBeanMapper beanMapper = new DozerBeanMapper();
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer-test.xml");
beanMapper.setMappingFiles(mappingFiles);
Map<String, String> rawData = new HashMap <String, String> ();
rawData.put("COMPANY_ASSG_ID","12345");
Hotel hotel = new Hotel();
beanMapper.map(rawData, hotel);
Try using this
instead of rawData
. Dozer would look for a read/write method for the field rawData
in hashmap and would end up with a mapping exception.
精彩评论