java - Using MapStruct to fill in same classes
问题描述
I am trying to use MapStructs to fill an object with values of a second object of the same class. My issue is that the source has some null values that are updating existing values in the target.
Class A{
Integer one;
Integer two;
}
@Mapper(componentModel="cdi")
Class Mapper{
public abstract void fill(A source, @MappingTarget A target);
}
@Test
void test(){
var source = new A(1, null);
var target = new A(null, 2);
mapper.fill(source, target);
//expected A(1,2) but get A(1, null)
}
is basically what I have.
I've tried adding NullValuePropertyMappingStrategy.IGNORE
to the Mapper, as well as annotating the fill method with @BeanMapping
to add the NullValuePropertyMappingStrategy.IGNORE
as well as the NullValueCheckStrategy.ALWAYS
but was unsuccessful.
解决方案
好的,所以我似乎已经解决了。我现在有
@Mapper(componentModel="cdi")
Class Mapper{
@BeanMapping(
nullValuePropertyMappingStrategy=NullValuePropertyMappingStrategy.IGNORE,
nullValueCheckStrategy=NullValueCheckStrategy.ALWAYS)
public abstract void fill(A source, @MappingTarget A target);
}
很可能我的项目没有正确构建,并且我的更正没有被部署。
推荐阅读
- networking - 无法从不同的子网访问某些设备
- matlab - Matlab - 使用预训练的 BodyPix 模型进行图像分割
- r - 通过在 R 中传递 2 个变量来提取数据帧的单个值
- c# - 如何在公共访问设置为“私有”的容器中复制 blob?
- python - 使用 csv 文件创建多个数据框并为其指定特定名称
- paypal - PayPal 获取退款交易的父交易
- sql - 如何在 SQL 中获取布尔值作为输出
- sql - SQL Server 阻止了对组件“xp_cmdshell”的过程“sys.xp_cmdshell”的访问,因为该组件作为安全配置的一部分被关闭
- mysql - 触发器中具有局部变量的算术
- ios - Flutter 启用手势导航 flutter_webview_plugin iOS