gpt4 book ai didi

Java:未映射的目标属性

转载 作者:行者123 更新时间:2023-12-01 14:06:41 25 4
gpt4 key购买 nike

我对Mapper有问题。我正在使用mapstruct-processor来构建Maven项目。每当我收到警告时:
警告:(15,16)java:未映射的目标属性:“从,到”。
警告:(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);
}

最佳答案

错误 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 。在文档中。有关 @InheritInverseConfiguration 的更多信息,请查看 here

如果 MessageMessageDTO 中的属性(getter/setter)名称相同,则不需要 @Mapping 注释

关于Java:未映射的目标属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45482677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com