miau
miau

Reputation: 227

Have a mapper not map a field in particular conditions

I have a very particular use case where I have in my hands an object a of class A and an object b of class B, such that a.b.equals(b) and I intend to map a to class C using Orika. The tricky bit is, I'd like to prevent a.b from being passed along and rather insert it by myself (a.b is actually fetched by JPA during the mapping which I'd like not to happen). On the other hand, I'd like to have a.b mapped in all other use cases.

Is there a decent way to implement that sort of behavior in Orika without specifying two custom ClassMaps?

Upvotes: 1

Views: 1092

Answers (2)

Adam Michalik
Adam Michalik

Reputation: 9945

One of the possible solutions is to use a 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();

See the "Customizing individual ClassMaps" section of the User Guide.

Upvotes: 2

hasnae
hasnae

Reputation: 2183

You can use ClassMapBuilder.exclude(String propertyName).

Upvotes: 0

Related Questions