[英]Set nested objects to null if IDs are null
我想將mapstruct配置為將一個ID設置為null的對象。 和/或如果他的所有字段均為空,則不要在第一個實例中初始化該對象。
現在,我這樣做:
public void setNestedObjectsToNullIfIdsNull(Servicio entity) {
if(entity == null) return;
if(entity.getViaje() != null && entity.getViaje().getId() == null) {
entity.setViaje(null);
}
if(entity.getPaciente() != null && entity.getPaciente().getId() == null) {
entity.setPaciente(null);
}
//... this is bad
}
我這樣做是因為如果已將關系對象初始化且其ID為null,則無法將實體插入數據庫。
不幸的是,這是MapStruct中的已知限制。 實現此目的的唯一方法是使用@AfterMapping
並將對象重置為null
。
正在尋找類似內容的MapStruct問題中已經存在此請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.