简体   繁体   English

如何在 MapStruct 中映射扩展类

[英]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 的默认实现假设如下:

  1. The type has a parameterless public static builder creation method that returns a builder.该类型具有返回构建器的无参数公共静态构建器创建方法。 So for example Person has a public static method that returns PersonBuilder.例如,Person 有一个返回 PersonBuilder 的公共静态方法。
  2. The builder type has a parameterless public method (build method) that returns the type being build In our example PersonBuilder has a method returning Person. builder 类型有一个无参数的公共方法(build 方法),它返回正在构建的类型。在我们的例子中,PersonBuilder 有一个返回 Person 的方法。
  3. In case there are multiple build methods, MapStruct will look for a method called build, if such method exists then this would be used, otherwise a compilation error would be created.如果有多个构建方法,MapStruct 将查找名为 build 的方法,如果存在这样的方法,则将使用该方法,否则将创建编译错误。

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.

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