如果 ID 为空,则将嵌套对象设置为空
Set nested objects to null if IDs are null
我想配置 mapstruct 以将对象设置为 null,如果他的 id 为 null。 And/or如果他的所有字段都为空,则不在第一个实例中初始化对象。
现在我这样做:
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 为空,我无法将实体插入数据库。
不幸的是,这是 MapStruct 中的一个已知限制。实现此目的的唯一方法是使用 @AfterMapping
并将对象重置为 null
.
在 MapStruct 问题中已经有 this 请求正在寻找类似的东西。
我想配置 mapstruct 以将对象设置为 null,如果他的 id 为 null。 And/or如果他的所有字段都为空,则不在第一个实例中初始化对象。
现在我这样做:
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 为空,我无法将实体插入数据库。
不幸的是,这是 MapStruct 中的一个已知限制。实现此目的的唯一方法是使用 @AfterMapping
并将对象重置为 null
.
在 MapStruct 问题中已经有 this 请求正在寻找类似的东西。