Question

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?

Était-ce utile?

La solution

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).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top