简体   繁体   English

Orika通用集合自定义映射

[英]Orika Generic Collection Custom Mapping

Orika has support for generic types, but I have trouble getting it to work with generic collections. Orika支持泛型类型,但是我很难使其与泛型集合一起使用。 Since Orika does not support different collection strategies (cumulative, non-cumulative, orphan removal) I need to write a custom mapper to handle my requirements. 由于Orika不支持不同的收集策略(累积,非累积,孤立删除),因此我需要编写一个自定义映射器来满足我的要求。

The problem is that Orika does not apply this mapper, but instead tries to use the normal collection mapping logic. 问题在于Orika没有应用此映射器,而是尝试使用常规的集合映射逻辑。

Type<List<Document>> DOCUMENT_LIST = new TypeBuilder<List<Document>>() {}.build();
Type<List<DocumentRepresentation>> DOCUMENT_REP_LIST = new TypeBuilder<List<DocumentRepresentation>>() {}.build();

mapperFactory.classMap(DOCUMENT_LIST, DOCUMENT_REP_LIST)
                .mapNulls(true)
                .mapNullsInReverse(true)
                .customize(new NonCumulativeListMapperDocumentToDocumentRepresentation())
                .register();

public class NonCumulativeListMapperDocumentToDocumentRepresentation
        extends CustomMapper<List<Document>, List<DocumentRepresentation>> {
    //mapping logic
}

I also tried to explicitly setting the type list in the parent mappings 我还尝试在父映射中显式设置类型列表

.fieldMap("documents", "documents")
.aElementType(Document.class)
.bElementType(DocumentRepresentation.class)
.add()

but this was also not picked up. 但这也没有得到解决。

Any hints as to what I'm missing? 关于我所缺少的任何提示吗?

This could be done by registering your custom mapper: 这可以通过注册您的自定义映射器来完成:

mapperFactory.registerMapper(new NonCumulativeListMapperDocumentToDocumentRepresentation());

And it will be used later when Orika have to map DOCUMENT_LIST DOCUMENT_REP_LIST. 当Orika必须映射DOCUMENT_LIST DOCUMENT_REP_LIST时,它将在以后使用。 The last fieldMap configuration is not needed. 不需要最后的fieldMap配置。

For more information about Merging collections in Orika, please refer to this simple test (CustomMergerTest) . 有关在Orika中合并集合的更多信息,请参考此简单测试(CustomMergerTest)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM