How to map Enum value to property of other java bean.

4,712 views
Skip to first unread message

sheka...@gmail.com

unread,
Feb 23, 2017, 8:43:43 AM2/23/17
to mapstruct-users

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.

Andreas Gudian

unread,
Feb 23, 2017, 12:51:22 PM2/23/17
to mapstruct-users, sheka...@gmail.com
Hi,

Directly usimg a public field is a new feature in MapStruct 1.2.0.Beta1 - are you using that version? If so, please open a new issue on GitHub, as I would expect that to work... 😉
If you don't use the latest beta, then you need a setter-method for operatingModelEntity...

Andreas

--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To post to this group, send email to mapstru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sheka...@gmail.com

unread,
Feb 24, 2017, 1:07:29 AM2/24/17
to mapstruct-users, sheka...@gmail.com

Thanks Andreas. I am using MapStruct 1.1.0.Final version.Can please elaborate the point you mentioned setter-method for operatingModelEntity or give me reference document where it is mentioned so i can follow.

Thanks,
Shekar.

Gunnar Morling

unread,
Feb 24, 2017, 2:16:59 AM2/24/17
to sheka...@gmail.com, mapstruct-users
You need to declare a setter ItemEntity#setOperatingModelEntity() instead of having the public field operatingModelEntity. The latter is only supported in MapStruct 1.2.

--Gunnar


To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-users+unsubscribe@googlegroups.com.
To post to this group, send email to mapstruct-users@googlegroups.com.

sheka...@gmail.com

unread,
Feb 24, 2017, 9:11:58 AM2/24/17
to mapstruct-users, sheka...@gmail.com

Thank you so much Andreas and Gunnar. Compile time error got resolved now.So I mentioned above, implemented custom mapping method to map ENUM to other java bean using MapStruct 1.1.0.Final version,Can i meet my requirement with out implementing custom mappings method.

Thanks,
Shekar.

Gunnar Morling

unread,
Feb 24, 2017, 5:06:43 PM2/24/17
to mapstruct-users, sheka...@gmail.com
Implementing a custom mapping method is the right way to do it.

--Gunnar
Reply all
Reply to author
Forward
0 new messages