Default for a null @MappingTarget value?

1,782 views
Skip to first unread message

Zachary Canann

unread,
Dec 22, 2016, 6:45:12 PM12/22/16
to mapstruct-users
If the target of a @MappingTarget annotation is null, is there a MapStruct feature that can be used to instantiate the object automatically?

We are trying to use MapStruct to update an existing object, where some of the sub-objects in the hierarchy may be set to null.

Gunnar Morling

unread,
Dec 23, 2016, 2:34:41 AM12/23/16
to Zachary Canann, mapstruct-users
I don't think update methods would instantiate any missing objects
right now. It seems a reasonable option, though, maybe controlled via
a new property on @MappingTarget?

Sjaak et al., WDYT?
> --
> You received this message because you are subscribed to the Google Groups
> "mapstruct-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mapstruct-use...@googlegroups.com.
> To post to this group, send email to mapstru...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Andreas Gudian

unread,
Dec 23, 2016, 11:21:35 AM12/23/16
to Gunnar Morling, Zachary Canann, mapstruct-users
Well, on non-iterable properties, any "null" targets properties are created before calling the property mapping method for it.

For example (taken from one of our tests):

        if ( source.getCompany() != null ) {
            if ( target.getCompany() == null ) {
                target.setCompany( new CompanyEntity() );
            }
            toCompanyEntity( source.getCompany(), target.getCompany() );
        }
        else {
            target.setCompany( null );
        }


Limitation: iterables and maps behave differently here, as we don't call property mapping methods with those types as @MappingTarget. So any updating logic would break below such property paths. The main reason for that is that we didn't yet came up with a clean solution to identify what elements in those collections need to be removed, created, or updated by the incoming source elements...

@Sjaak, did I miss something?



2016-12-23 8:34 GMT+01:00 Gunnar Morling <gun...@hibernate.org>:
I don't think update methods would instantiate any missing objects
right now. It seems a reasonable option, though, maybe controlled via
a new property on @MappingTarget?

Sjaak et al., WDYT?

2016-12-23 0:45 GMT+01:00 Zachary Canann <zachar...@gmail.com>:
> If the target of a @MappingTarget annotation is null, is there a MapStruct
> feature that can be used to instantiate the object automatically?
>
> We are trying to use MapStruct to update an existing object, where some of
> the sub-objects in the hierarchy may be set to null.
>
> --
> You received this message because you are subscribed to the Google Groups
> "mapstruct-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mapstruct-users+unsubscribe@googlegroups.com.
> To post to this group, send email to mapstruct-users@googlegroups.com.

> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-users+unsubscribe@googlegroups.com.
To post to this group, send email to mapstruct-users@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages