我有一个非常特殊的用例,我手中有一个类a
的对象A
和一个类b
的对象B
,这样a.b.equals(b)
和我打算将映射到类a
使用Orika。
棘手的一点是,我想防止传递C
,而是自己插入它(a.b
实际上是在我不想发生的映射过程中由JPA提取的)。另一方面,我想在所有其他用例中都映射a.b
。
是否有一种体面的方式在Orika中实现这种行为而无需指定两个自定义a.b
?
最佳答案
可能的解决方案之一是使用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();
请参见User Guide的“自定义单个ClassMap”部分。