简体   繁体   English

推土机-从实体列表到实体ID列表

[英]Dozer - From List of entities to list of entities' id

I have a domain object called User: 我有一个名为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 我有相关的DTO(UserDTO)

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. 我想使用推土机从域对象转换为DTO。 The Profile class has a property Profile类具有一个属性

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. 我想要的是,Dozer会获取列表中每个配置文件的配置文件ID,并将其保存在DTO的列表中。 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>

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. 这表示推土机使用哪种方法进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM