childrenobjects的MapStruct泛型Map和map组合列表
MapStruct generic Map and map combined list of children objects
我有 parent class : User.java
和 2 classes : FacebookUser.java
和 TwitterUser.java
它们是返回的实体取决于数据库中使用 DiscriminatorColumn
的类型列,我想编写正确的映射器来映射可能是 FacebookUser 或 TwitterUser 实例的用户。我有以下映射器似乎无法按预期工作,仅映射 User
parent 而不是 children:
@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);
我只有 firstName
和 lastName
但没有 screenName
或 userId
.
有什么解决办法吗?
目前,它似乎还不能作为 mapstruct 的功能使用:Support for Type-Refinement mapping (or Downcast Mapping)
我在他们的 google 组中问了这个问题:https://groups.google.com/forum/?fromgroups#!topic/mapstruct-users/PqB-g1SBTPg
发现我需要在界面内使用 default
方法进行手动映射(对于 java 8)。
还有另一个几乎不适用的映射父对象的问题,所以我又写了一个空 class 父对象 class 的子对象称为 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);
}
}
}
我有 parent class : User.java
和 2 classes : FacebookUser.java
和 TwitterUser.java
它们是返回的实体取决于数据库中使用 DiscriminatorColumn
的类型列,我想编写正确的映射器来映射可能是 FacebookUser 或 TwitterUser 实例的用户。我有以下映射器似乎无法按预期工作,仅映射 User
parent 而不是 children:
@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);
我只有 firstName
和 lastName
但没有 screenName
或 userId
.
有什么解决办法吗?
目前,它似乎还不能作为 mapstruct 的功能使用:Support for Type-Refinement mapping (or Downcast Mapping)
我在他们的 google 组中问了这个问题:https://groups.google.com/forum/?fromgroups#!topic/mapstruct-users/PqB-g1SBTPg
发现我需要在界面内使用 default
方法进行手动映射(对于 java 8)。
还有另一个几乎不适用的映射父对象的问题,所以我又写了一个空 class 父对象 class 的子对象称为 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);
}
}
}