Hi team,
I have requirement of mapping one enum to property of the other java bean.I have seen the Enum to Enum mapping in the document but could not find how to map from Enum to other java bean.
Example:
My object hierarchy is like below
DTO : Item->OperatingModel(which is Enum)
Entity: ItemEntity->OperatingModelEntity(maintained a separate java bean as i have to persist in separate table)
Here is piece of code.Please help in mapping OperatingModel value in Item DTO to property(code) of OperatingModelEntity object.
I am not sure if i use custom methods to mappers like below, but when i tried to use getting error as "property OperatingModelEntity has no write accessor "
@Mapper
public interface EnumMapper {
EnumMapper INSTANCE = Mappers.getMapper(EnumMapper.class );
@Mapping(source = "operationModel",target = "operatingModelEntity")
ItemEntity mapItemToItemEntity(Item item);
default OperatingModelEntity OperatingModelToOperatingModelEntity(OperatingModel operatingModel){
OperatingModelEntity ome = new OperatingModelEntity();
ome.setCode(operatingModel.getValue());
return ome;
}
}
public class Item {
public int id;
public OperatingModel operationModel;
private String description;
//setters and getters
}
public enum OperatingModel {
ONSITE("ON_VAL"),
OFFSHORE("OFF_VAL");
private String value;
OperatingModel(String value) {
this.value = value;
}
public String getValue(){
return this.value;
}
}
@Entity
@Table(name = "item")
public class ItemEntity {
public int id;
public OperatingModelEntity operatingModelEntity;
private String description;
//setters and getters
}
@Entity
@Table(name = "operating_model")
public class OperatingModelEntity {
private static final long serialVersionUID = 1L;
private Short id;
private String code;
private String description;
}
Thanks,
Shekar.