我有一个非常特殊的用例,我手中有一个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();
请参阅 User Guide 的“自定义单个 ClassMap”部分.
关于java - 让映射器在特定条件下不映射字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33262462/