[英]How to map extended classes in MapStruct
Gotta question regarding mapStruct.有关于 mapStruct 的问题。 I have case where I extend class from base entity and not sure how to map it.
我有从基本实体扩展类的情况,但不确定如何映射它。 Here is my case.
这是我的情况。
BaseEntity:基础实体:
public class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id")
private Long id;
}
BaseDto:基点:
public class BaseDto {
private Long id;
}
UserEntity:用户实体:
public class User extends BaseEntity {
private String name;
private String lastName;
private String username;
private String password;
private String profilePicturePath;
}
UserDto:用户名:
public class UserDto extends BaseDto {
private String name;
private String lastName;
private String username;
private String password;
private String profilePicturePath;
}
And mapper is like this:而映射器是这样的:
@Mapper(uses = {BaseMapper.class})
public interface UserMapper {
User userDtoToUser(UserDto userDto);
UserDto userToUserDto(User user);
}
BaseMapper:基础映射器:
@Mapper
public interface BaseMapper {
BaseEntity dtoToEntity(BaseDto baseDto);
BaseDto entityToDto(BaseEntity baseEntity);
}
Problem is that I don't get ID property mapped.问题是我没有映射 ID 属性。
Thank you for your time.感谢您的时间。
EDIT:编辑:
There is no error shown, in mapper implementation (generated code) there is no mapping for that ID:没有显示错误,在映射器实现(生成的代码)中没有该 ID 的映射:
@Override
public User userDtoToUser(UserDto userDto) {
if ( userDto == null ) {
return null;
}
UserBuilder user = User.builder();
user.name( userDto.getName() );
user.lastName( userDto.getLastName() );
user.username( userDto.getUsername() );
user.password( userDto.getPassword() );
user.profilePicturePath( userDto.getProfilePicturePath() );
return user.build();
}
I'm guessing (as you have not put buider
code) the problem is that your builder class does not include parent class field.我猜(因为您没有放置
buider
代码)问题是您的构建器类不包含父类字段。 MapStruct makes some assumption while generating code for mapper. MapStruct 在为映射器生成代码时做了一些假设。 From documentation -
从文档-
The default implementation of the BuilderProvider assumes the following:
BuilderProvider 的默认实现假设如下:
If you are using Lombok, you can solve this by using @SuperBuilder
as -如果您使用的是 Lombok,则可以通过使用
@SuperBuilder
来解决此问题 -
@SuperBuilder
@Getter
@ToString
public class UserDto extends BaseDto {
private String name;
private String lastName;
private String username;
private String password;
private String profilePicturePath;
}
@Getter
@SuperBuilder
class BaseDto {
private Long id;
}
@SuperBuilder
@Getter
@ToString
public class User extends BaseEntity {
private String name;
private String lastName;
private String username;
private String password;
private String profilePicturePath;
}
@Setter
@Getter
@SuperBuilder
class BaseEntity {
private Long id;
}
And generated could looks like -生成的可能看起来像 -
@Override
public User userDtoToUser(UserDto userDto) {
if ( userDto == null ) {
return null;
}
UserBuilder<?, ?> user = User.builder();
user.id( userDto.getId() );
user.name( userDto.getName() );
user.lastName( userDto.getLastName() );
user.username( userDto.getUsername() );
user.password( userDto.getPassword() );
user.profilePicturePath( userDto.getProfilePicturePath() );
return user.build();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.