如何将 HashMap 映射到 Pojo

发布于 2024-09-30 01:35:51 字数 1272 浏览 4 评论 0原文

我在使用推土机将 HashMap 映射到 Pojo 时遇到困难。注意:我的 Pojo 中的属性名称不会映射到我填充的 ​​HashMap 中的键值。我正在尝试使用 dozer 映射文件中的 set-method 属性将哈希键映射到 Pojo setter。当我运行下面的代码时,我没有收到任何异常,但 Pojo 没有填充哈希中的任何数据。任何建议或反馈将不胜感激。

Pojo:

public class Hotel {

private String companyAssignedId;

public Hotel(){}

public String getCompanyAssignedId() {
    return companyAssignedId;
}

public void setCompanyAssignedId(String companyAssignedId) {
    this.companyAssignedId = companyAssignedId;
}

}

映射 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>
        <b key="COMPANY_ASSG_ID">rawData</b>
   </field>
</mapping>

执行代码:

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);

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>
        <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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池予 2024-10-07 01:35:51

尝试使用 this 而不是 rawData。 Dozer 会在 hashmap 中寻找 rawData 字段的读/写方法,最终会出现映射异常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文