我有一个非常特殊的用例,我手中有一个a
类对象A
和一个类对象b
,B
这样a.b.equals(b)
我打算使用 Orika映射a
到类。C
棘手的一点是,我想防止a.b
被传递,而是自己插入它(a.b
实际上是在我不想发生的映射期间由 JPA 获取的)。另一方面,我想a.b
在所有其他用例中进行映射。
有没有一种体面的方法可以在不指定两个自定义的情况下在 Orika 中实现这种行为ClassMap
?
一种可能的解决方案是使用CustomMapper
:
mapperFactory.classMap(A.class, C.class)
.byDefault() // or your explicit mappings
.exclude("b") // exclude the property from default mapping
.customize(
new CustomMapper<A, C> {
public void mapAtoB(A a, C c, MappingContext context) {
// implement your own logic for mapping the b property
}
}
.register();
请参阅用户指南的“自定义单个 ClassMap”部分。
您可以使用 ClassMapBuilder.exclude(String propertyName)。