ObjectFactory not used in when using MappingTarget

164 views
Skip to first unread message

jacob...@gmail.com

unread,
Sep 16, 2020, 5:22:33 PM9/16/20
to mapstruct-users
I have the following setup:
EntityA has an EnttiyB as a property; entityB uses the objectFactory to populate one of its own properties.

The entityAMapper uses the entityBMapper.

This works fine for the normal entityA toEntity( entityAResource) method.
The generated code uses the entityBMapper which uses the object factory to resolve the property.

My entityA also has an 'update' method which uses the @MappingTarget annotation:
entityA update( EntityAResource resource, @MappingTarget entityA)

The now generated code does not use the entityBMapper anymore, instead it does a 
new EntityB()
and calls a generated method to set other properties.
Since this is not calling the objectFactory, it blows up.

Is there some magic I need to do to have a mappingTarget working with a mapper?



jacob...@gmail.com

unread,
Sep 17, 2020, 12:00:18 PM9/17/20
to mapstruct-users
So not sure if this is a bug or not.  For my update mapping I hinted to ignore 1 attribute of entityB.  

@Mapping( "target = "entityB.someProperty", ignore = true)

entityA update( EntityAResource resource, @MappingTarget entityA) 
 
With the ignore this code gets generated:

        if ( resource.getEntityB() != null ) {
            if ( entityA.getEntityB    () == null ) {
                 entityA  .setEntityB    ( new  entityB  () );
            }
            entityB( resource.getentityB  (), entityA. entityB    () );   // 'entityB' is some private generated method
        }

When I remove the ignore, the generated code now uses the mapper instead and the above becomes:

        entityA.setEntityB(entityBMapper.mapResource( resource.getEntityB() ) );




Reply all
Reply to author
Forward
0 new messages