Prasann
Prasann

Reputation: 1291

How to access a DTO attribute when DTO is inside a hashmap in DOZER mapping file

I have a UserDTO which has userID field. The HashMap has this DTO as value for key User_Details.

I want to use DOZER mapping to set the userID attribute from HashMap->User_Details->userId to attribute UserDisplayDTO->userId.

How can I do this in Dozer XML mapping?

<mapping map-id="testMapping">
    <class-a>java.util.HashMap</class-a>
    <class-b>com.common.dto.UserDisplayDTO</class-b>
    <field>
        <a key="User_Details">this</a>
        <b>userId</b>
    </field>
</mapping>

Upvotes: 1

Views: 638

Answers (1)

Uwe
Uwe

Reputation: 436

You have to define a custom converter for this. Atm, dozer xml mapping doesn't support a keybased hashmap lookup.

So for your case, you need something like

<field custom-converter="com.your.custom.converter.UserIdConverter">
    <a>hashmapfield</a>
    <b>userId</b>
</field>

In the UserIdConverter implementation, you would have to retrieve the value from the hashmap and return it (null checking etc. omitted for the sake of clarity):

@Override
public Long convertTo(HashMap map, Long userId) {
    UserDTO dto = (UserDTO)map.get("User_Details");
    return dto.getUserId();
}

Upvotes: 2

Related Questions