2

I have a domain object called User:

public class User {
    private long id;
    private String username;
    private String email;
    private List<Profile> profiles;

    // getters & setters
}

And I have the related DTO (UserDTO) which is

public class UserDTO {
    private long id;
    private String username;
    private String email;
    private List<Long> profilesId;

    // getters & setters
 }

I'd like to use Dozer to convert from domain object to DTO. The Profile class has a property

Long id;

What I want is that Dozer takes the profile's id for each profile in the list and save it in the DTO's list. Can I do something like that? Do I have to use custom converters?

Here's my actual mapping file

<mapping>
    <class-a>common.model.User</class-a>
    <class-b>common.model.dto.UserDTO</class-b>
    <field>
        <a>legalEntity.id</a>
        <b>legalEntityId</b>
    </field>
    <field type="one-way">
        <a>profiles.id</a>
        <b>profilesId</b>
    </field>
</mapping>
dylaniato
  • 516
  • 9
  • 23

1 Answers1

0

Solved just add to the source class this method

public List<Long> getProfilesId() {
    List<Long> profilesId = new ArrayList<Long>();
    for(Profile p : this.profiles) {
        profilesId.add(p.getId());
    }
    return profilesId;
}

and to the mapping file

<field type="one-way">
    <a get-method="getProfilesId">profiles</a>
    <b>profilesId</b>
</field>

which says Dozer which method use to make the conversion.

dylaniato
  • 516
  • 9
  • 23