简体   繁体   English

无法使用 MapStruct 映射来自 Integer 的枚举

[英]Cannot mapping with MapStruct an Enum from Integer

I Have a problem attempting mapping to an ENUM from an Integer value, my method mapping its:我在尝试从 Integer 值映射到 ENUM 时遇到问题,我的方法映射它:

@Mapping(
      target = "myModel.states",// this is my ENUM
      source = "source.stateId") // this is the Integer Value
  ClsTargetModel mapCreditCard(ClsMyClass source);

ENUM and entity model:枚举和实体 model:

Entity:实体:

@Getter
@Setter
@Entity
@Table(name = "my_table")
public class MyEntityModel {
  @Id
  @Column(name = "id")
  private Integer id;

  @Basic
  @Column(name = "description")
  private String description;
}

ENUM:枚举:

@Getter
@ToString
public enum EnumStates {
  STATE1(1),
  STATE2(2),
  STATE3(3);

  public Integer id;

  EnumStates(Integer id) {
    this.id = id;
  }

  public static EnumStates getStateById(Integer stateId) {
    return Arrays.stream(EnumStates.values())
        .filter(enumStateValue -> enumStateValue.id == stateId)
        .findFirst()
        .orElse(null);
  }
}

When Im trying of mapping the ID from Model Entity to the ENUM, then mapstruct show me an error:当我尝试将 ID 从 Model 实体映射到 ENUM 时,mapstruct 显示错误:

error: Can't map property "java.lang.Integer Id" to "EnumStates states". Consider to declare/implement a mapping method: "EnumStates map(java.lang.Integer value)".

I think that the method was declare in the enum, but mapstruct always responds that error, can you check my code please?我认为该方法是在枚举中声明的,但是 mapstruct 总是响应该错误,您能检查我的代码吗?

You need to tell mapstruct how to map from an integer value to your enum.您需要告诉 mapstruct 如何将 map 从 integer 值转换为您的枚举。

A possible solution could be:一个可能的解决方案可能是:

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper
public interface ClsMapper {

    @Mapping(
            target = "states",// this is my ENUM
            source = "id") //
    ClsTargetModel to(ClsMyClass clsMyClass);

    default EnumStates map(int value) {
        return EnumStates.getStateById(value);
    }
}

Check that I have created a map method where I tell mapstruct how to convert an int to a EnumStates.检查我是否创建了一个 map 方法,我告诉 mapstruct 如何将 int 转换为 EnumStates。 Using the conversion method you created inside the enum.使用您在枚举中创建的转换方法。

I have simplified the model classes as I am not using lombok nor database for this example.我已经简化了 model 类,因为在这个例子中我没有使用 lombok 或数据库。 It is pretty much same code as yours I didn't change any logic.它与您的代码几乎相同,我没有更改任何逻辑。

public class ClsMyClass {
    private int id;
    private String description;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
public class ClsTargetModel {
    private EnumStates states;

    public EnumStates getStates() {
        return states;
    }

    public void setStates(EnumStates states) {
        this.states = states;
    }
}
public enum EnumStates {
    STATE1(1),
    STATE2(2),
    STATE3(3);

    public Integer id;

    EnumStates(Integer id) {
        this.id = id;
    }

    public static EnumStates getStateById(Integer stateId) {
        return Arrays.stream(EnumStates.values())
                .filter(enumStateValue -> enumStateValue.id == stateId)
                .findFirst()
                .orElse(null);
    }
}

Then mapstruct autogenerate a mapper code like:然后 mapstruct 自动生成一个映射器代码,如:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2020-07-26T14:31:58+0200",
    comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_144 (Oracle Corporation)"
)
public class ClsMapperImpl implements ClsMapper {

    @Override
    public ClsTargetModel to(ClsMyClass clsMyClass) {
        if ( clsMyClass == null ) {
            return null;
        }

        ClsTargetModel clsTargetModel = new ClsTargetModel();

        clsTargetModel.setStates( map( clsMyClass.getId() ) );

        return clsTargetModel;
    }
}

I hope this solves your issue, let me know in case of any doubt.我希望这能解决您的问题,如有任何疑问,请告诉我。

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

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