Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Unmapped target properties

I have a problem with Mapper.I am using a mapstruct-processor to build Maven project. All time I get a warning: Warning:(15, 16) java: Unmapped target properties: "from, to". Warning:(13, 13) java: Unmapped target properties: "clientFrom, clientTo". What did I suppose to do with that? Class Client works well and creates a Client.

 @Entity
public class Message {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String message;

@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "client_from")
private Client clientFrom;

@ManyToOne
@JoinColumn(name = "client_to")
private Client clientTo;

public Message(){}

public Long getId() {
    return id;
}

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

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public Client getClientFrom() {
    return clientFrom;
}

public void setClientFrom(Client clientFrom) {
    this.clientFrom = clientFrom;
}

public Client getClientTo() {
    return clientTo;
}

public void setClientTo(Client clientTo) {
    this.clientTo = clientTo;
}


}


public class MessageDTO {

private Long id;
private String message;
private ClientDTO from;
private ClientDTO to;

public MessageDTO(){}

public Long getId() {
    return id;
}

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

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public ClientDTO getFrom() {
    return from;
}

public void setFrom(ClientDTO from) {
    this.from = from;
}

public ClientDTO getTo() {
    return to;
}

public void setTo(ClientDTO to) {
    this.to = to;
}
}



 @Mapper(componentModel = "spring", uses = {ClientMapper.class})
public interface MessageMapper {

Message toMessage(MessageDTO messageDTO);

MessageDTO toMessageDTO(Message message);

List<Message> toMessageList(List<MessageDTO> list);
List<MessageDTO> toMessageDTOList(List<Message> list);
}
like image 447
Mike Evans Avatar asked Aug 03 '17 11:08

Mike Evans


2 Answers

The error Unmapped target properties: "<list of properties>" is usually represented on the appropriate methods and in your case it means that MapStruct could not find from and to for the mapping of MessageDTO toMessageDTO(Message message) and clientFrom and clientTo for the mapping of Message toMessage(MessageDTO messageDTO).

To solve this you will have to explicitly define the mappings between the different properties.

Your mapper will look like (I am assuming mapstruct-jdk8 usage):

@Mapper(componentModel = "spring", uses = {ClientMapper.class})
public interface MessageMapper {

    @Mapping(target = "clientFrom", source = "from")
    @Mapping(target = "clientTo", source = "to")
    Message toMessage(MessageDTO messageDTO);

    @InheritInverseConfiguration //MapStruct will automatically reverse the defined mappings from toMessage
    MessageDTO toMessageDTO(Message message);

    List<Message> toMessageList(List<MessageDTO> list);
    List<MessageDTO> toMessageDTOList(List<Message> list);
}

For better understanding have a look at here. In the documentation. For more info about the @InheritInverseConfiguration have a look here.

If the names of the properties (getters / setters) were the same in Message and MessageDTO, then you wouldn't need the @Mapping annotations

like image 105
Filip Avatar answered Oct 03 '22 09:10

Filip


In latest versions of MapStruct you can:

  1. define @Mapping annotation with ignore=true attribute for any field that should not be mapped OR
  2. add special reporting policy to your mapper interface to suppress warnings on all unmapped fields at once, like this:
    @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
like image 33
Anatoly Tutov Avatar answered Oct 03 '22 10:10

Anatoly Tutov