Java:未映射的目标属性

时间:2017-08-03 11:18:45

标签: java maven mapper mapstruct

我遇到Mapper问题。我正在使用mapstruct-processor来构建Maven项目。我一直收到警告: 警告:(15,16)java:未映射的目标属性:" from,to"。 警告:(13,13)java:未映射的目标属性:" clientFrom,clientTo"。 我该怎么办呢?类客户端运行良好并创建客户端。

 @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);
}

2 个答案:

答案 0 :(得分:2)

错误Unmapped target properties: "<list of properties>"通常表示在相应的方法上,在您的情况下,这意味着MapStruct无法找到fromto来映射MessageDTO toMessageDTO(Message message)和{ {1}}和clientFrom用于映射clientTo

要解决此问题,您必须明确定义不同属性之间的映射。

您的映射器看起来像(我假设使用Message toMessage(MessageDTO messageDTO)):

mapstruct-jdk8

为了更好地理解,请查看here。在文档中。有关@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); } 的详情,请查看here

如果@InheritInverseConfigurationMessage中的属性名称(getter / setter)相同,那么您就不需要MessageDTO注释

答案 1 :(得分:2)

在最新版本的MapStruct中,您可以:

  1. 使用@ ignore = true属性为任何字段定义@Mapping注释 不应该被映射或
  2. 向您的映射器界面添加特殊的报告策略,以一次禁止所有未映射字段的警告,例如:
    @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)