I Have a problem attempting mapping to an ENUM from an Integer value, my method mapping its:
@Mapping(
target = "myModel.states",// this is my ENUM
source = "source.stateId") // this is the Integer Value
ClsTargetModel mapCreditCard(ClsMyClass source);
ENUM and entity 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:
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 ?