简体   繁体   中英

Orika Generic Collection Custom Mapping

Orika has support for generic types, but I have trouble getting it to work with generic collections. Since Orika does not support different collection strategies (cumulative, non-cumulative, orphan removal) I need to write a custom mapper to handle my requirements.

The problem is that Orika does not apply this mapper, but instead tries to use the normal collection mapping logic.

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. The last fieldMap configuration is not needed.

For more information about Merging collections in Orika, please refer to this simple test (CustomMergerTest) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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