2

I want to map two object with mapStruct

public class EntityA
{

   @Id
   private String id;
   @Column
   private String anotherField

The second object with an object a paramter

public class ABC
{
   private final ObjectXYZ identification;
   // another fields
public class ObjectXYZ 
{
   private final String identification;

My Mapper:

@Mapper
public interface ObjectMapper
{
   @Mapping(target = "identification", source = "identification", qualifiedByName = "ObjectXYZ")
   ABC toABC(EntityA entity);

   EntityA toEntityA(Material material);

   @Named("ObjectXYZ")
   @Mapping(target = "identification", source = "identification")
   ObjectXYZ toObjectXYZ(String identification);
}

how can i map the EntityA.id to ABC.identification.identification

emoleumassi
  • 4,881
  • 13
  • 67
  • 93

1 Answers1

1

Objects definition:

public class EntityA {

    private String id;

    private String anotherField;

    public String getId() {
        return id;
    }

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

    public String getAnotherField() {
        return anotherField;
    }
}

public class ABC {
    private final ObjectXYZ identification;
    private final String anotherField;

    public ABC(ObjectXYZ identification, String anotherField) {
        this.identification = identification;
        this.anotherField = anotherField;
    }

    public String getAnotherField() {
        return anotherField;
    }

    public ObjectXYZ getIdentification() {
        return identification;
    }
}

public class ObjectXYZ
{
    private  String identification;

    public ObjectXYZ(String identification) {
        this.identification = identification;
    }

    public String getIdentification() {
        return identification;
    }
}

Mapper:
Mapstruct automatically determines the methods or constructors for mapping, you need just specify the right target\source properties.

@Mapper
public interface ObjectMapper
{
    @Mapping(target = "identification", source = "entity.id")
    @Mapping(target = "anotherField", source = "entity.anotherField")
    ABC toABC(EntityA entity);

    @Mapping(target = "identification", source = "identification")
    ObjectXYZ toObjectXYZ(String identification);
}

Generated code:

public class ObjectMapperImpl implements ObjectMapper {

    @Override
    public ABC toABC(EntityA entity) {
        if ( entity == null ) {
            return null;
        }

        ObjectXYZ identification = null;
        String anotherField = null;

        identification = toObjectXYZ( entity.getId() );
        anotherField = entity.getAnotherField();

        ABC aBC = new ABC( identification, anotherField );

        return aBC;
    }

    @Override
    public ObjectXYZ toObjectXYZ(String identification) {
        if ( identification == null ) {
            return null;
        }

        String identification1 = null;

        identification1 = identification;

        ObjectXYZ objectXYZ = new ObjectXYZ( identification1 );

        return objectXYZ;
    }
}
Eugene
  • 5,269
  • 2
  • 14
  • 22