Java:未映射的目标属性

Java: Unmapped target properties

我在使用 mapstruct-processor 构建 Maven 项目时遇到问题 Mapper.I。每次我都会收到警告: 警告:(15, 16) java:未映射的目标属性:"from, to"。 警告:(13, 13) java:未映射的目标属性:"clientFrom, clientTo"。 我应该怎么办? Class客户端运行良好,创建了一个客户端。

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

错误 Unmapped target properties: "<list of properties>" 通常在适当的方法上表示,在您的情况下,这意味着 MapStruct 找不到 fromto 用于 MessageDTO toMessageDTO(Message message) 的映射clientFromclientTo 用于 Message toMessage(MessageDTO messageDTO).

的映射

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

你的映射器看起来像(我假设 mapstruct-jdk8 用法):

@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. In the documentation. For more info about the @InheritInverseConfiguration have a look here

如果 MessageMessageDTO 中的属性名称(getters / setters)相同,那么你就不需要 @Mapping 注释

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

  1. 为任何字段定义带有 ignore=true 属性的 @Mapping 注释 不应映射的 OR
  2. 向您的映射器界面添加特殊报告策略,以立即禁止所有未映射字段上的警告,如下所示:
    @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)