-1

Suppose you have two entities defining objects in a database Ent1 and Ent2 and a DTO describing two in one.
My Mapper with MapStruct EntitiesDtoMapper looks like :

@Mapper
interface EntitiesDtoMapper{
    DTO EntitiesToDto(Ent1 ent1, Ent2 ent2);
    //It is possible to do this?
    Ent1 DtoToEnt1(DTO dto);
}

I like to get Ent1 and Ent2 from a DTO, it's possible?

Ehcnalb
  • 466
  • 4
  • 16
  • Have you tried this? What does MapStruct do when you have such mapper. The question is a big vague as trying it out would show you if it will work or not. – Filip Jan 05 '19 at 08:25
  • I tried but I have an AbstractMethodError, I forgot the mapstruct-processor, But it's OK it's working – Ehcnalb Jan 07 '19 at 14:08

2 Answers2

0

I suppose that separation of one DTO into two entities if they are not embedded is not the best practice. If so, I suggest to map dto into two entities manually because it is the easiest and fastest way.

Echoinacup
  • 482
  • 4
  • 12
0

To whom are interested; it's possible, but beware with the names of parameters of Entities.
Here an exemple :

@Data @Entity
public class Ent1{
  public Ent1() {}
  String id;
  String name;
}

@Data @Entity
public class Ent2{
  public Ent2() {}
  String id;
  String name;
}

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DTO{
  String id1,id2,name1,name2;
}

@Mapper
public interface EntitiesDtoMapper{  

  EntitiesIcspDtoMapper INSTANCE = Mappers.getMapper(EntitiesDtoMapper.class);

  @Mappings({
    @Mapping(source="ent1.id", target = "id1"),
    @Mapping(source="ent1.name", target = "name1"),
    @Mapping(source="ent2.id", target = "id2"),
    @Mapping(source="ent2.name", target = "name2")
  })
  DTO EntitiesToDto(Ent1 ent1, Ent2 ent2);

  @Mappings({
    @Mapping(source="id1", target = "id"),
    @Mapping(source="name1", target = "name"),
  })
  Ent1 DtoToEnt1(DTO dto);

  @Mappings({
    @Mapping(source="id2", target = "id"),
    @Mapping(source="name2", target = "name"),
  })
  Ent2 DtoToEnt2(DTO dto); 

}
Ehcnalb
  • 466
  • 4
  • 16