8

我有作为父类:User.java和 2 个类:FacebookUser.java它们TwitterUser.java是返回的实体,取决于使用数据库中的类型列DiscriminatorColumn,我想编写正确的映射器来映射可能是 FacebookUser 或 TwitterUser 实例的用户。我有以下似乎无法按预期工作的映射器,仅映射User父级而不是子级:

@Mapper
public interface UserMapper {
    public static UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    User map(UserDTO userDTO);

    @InheritInverseConfiguration
    UserDTO map(User user);

    List<UserDTO> map(List<User> users);

    FacebookUser map(FacebookUserDTO userDTO);

    @InheritInverseConfiguration
    FacebookUserDTO map(FacebookUser user);

    TwitterUser map(TwitterUserDTO userDTO);

    @InheritInverseConfiguration
    TwitterUserDTO map(TwitterUser user);

}

然后我使用:

UserDTO userDto = UserMapper.INSTANCE.map(user);

要映射的类:

@Entity
@Table(name = "users")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, length = 10)
@DiscriminatorValue(value = "Local")
public class User {
    @Column
    private String firstName;
    @Column
    private String lastName;
    ///... setters and getters
}

@Entity
@DiscriminatorValue(value = "Facebook")
public class FacebookUser extends User {
    @Column
    private String userId;
    ///... setters and getters
}

@Entity
@DiscriminatorValue(value = "Twitter")
public class TwitterUser extends User {
    @Column
    private String screenName; 
    ///... setters and getters
}

DTO:

public class UserDTO {
    private String firstName;
    private String lastName;
    ///... setters and getters
}

public class FacebookUserDTO extends UserDTO {
    private String userId;
    ///... setters and getters
}

public class TwitterUserDTO extends UserDTO {
    private String screenName; 
    ///... setters and getters
}

此外,如果我有与 Facebook 用户和 Twitter 用户或基本用户混合的用户列表:

假设我有以下用户:

User user = new User ("firstName","lastName");
User fbUser = new FacebookUser ("firstName","lastName","userId");
User twUser = new TwitterUser ("firstName","lastName","screenName");

List<User> users = new ArrayList<>();
users.add(user);
users.add(fbUser);
users.add(twUser);

//Then: 

List<UserDTO> dtos = UserMapper.INSTANCE.map(users);

我只得到firstNameandlastName但没有screenNameor userId

有什么解决办法吗?

4

1 回答 1

8

目前,它似乎还不能作为 mapstruct 的一个功能: 支持类型细化映射(或向下转换映射)

我在他们的谷歌组中问了这个问题:https ://groups.google.com/forum/?fromgroups#!topic/mapstruct-users/PqB-g1SBTPg

发现我需要使用default接口内的方法进行手动映射(对于java 8)。

并且遇到了几乎不适用的映射父级的另一个问题,因此我又编写了一个空类,该父类的子级称为LocalUserDTO

所以代码变成如下:

@Mapper
public interface UserMapper {
    public static UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    LocalUser map(LocalUserDTO userDTO);

    @InheritInverseConfiguration
    LocalUserDTO map(LocalUser user);


    List<UserDTO> map(List<User> users);

    FacebookUser map(FacebookUserDTO userDTO);

    @InheritInverseConfiguration
    FacebookUserDTO map(FacebookUser user);

    TwitterUser map(TwitterUserDTO userDTO);

    @InheritInverseConfiguration
    TwitterUserDTO map(TwitterUser user);

    default UserDTO map(User user) {

        if (user instanceof FacebookUser) {
            return this.map((FacebookUser) user);
        } else if (user instanceof TwitterUser) {
            return this.map((TwitterUser) user);
        } else {
            return this.map((LocalUser) user);
        }
    }

    @InheritInverseConfiguration
    default User map(UserDTO userDTO) {
        if (userDTO instanceof FacebookUserDTO) {
            return this.map((FacebookUserDTO) userDTO);
        } else if (userDTO instanceof TwitterUserDTO) {
            return this.map((TwitterUserDTO) userDTO);
        } else {
            return this.map((LocalUserDTO) userDTO);
        }
    }

}
于 2016-10-03T08:37:46.610 回答