JPA Lazy mapping

819 views
Skip to first unread message

Jorge Martins

unread,
Feb 25, 2016, 4:56:36 AM2/25/16
to mapstruct-users
I'm using mapstruct to map hibernate entities to DTO's and the inverse.
The mappings are working very well, however I'm still struggling with lazy/eager mappings.

In some cases I want to map a lighter version of the entity (without fetching collections). In some other cases I want to map fetching some of the collections.
To do that I was thinking to use different versions of mappings, ignoring the collections when I don't want to fetch.

I try to do that using custom qualifiers: @LazyMapping and @EagerMapping

@EagerMapping
@Mappings({
@Mapping(target = "description", source = "sampleNested.description"),
@Mapping(target = "attributes", source = "sampleNested.attributes"),
@Mapping(target = "somecollection", source = "collectionxpto") })
SampleDTO sampleDtoToEntity(SampleEntity sampleEntity);
@LazyMapping
@InheritConfiguration
@Mapping(target = "somecollection", ignore = true) //MARK AS IGNORED
SampleDTO lazySampleDtoToEntity(SampleEntity sampleEntity);
@IterableMapping(qualifiedBy=EagerMapping.class)
List<SampleDTO> samplesDtoToEntities(List<SampleEntity> sampleEntities);
@IterableMapping(qualifiedBy=LazyMapping.class)
List<SampleDTO> lazySamplesDtoToEntities(List<SampleEntity> sampleEntities);
However in some cases I'm getting error for ambiguos mappings.
Is this the correct approach? How can I do this with mapstruct?

Thank you

Gunnar Morling

unread,
Feb 25, 2016, 4:59:57 AM2/25/16
to Jorge Martins, mapstruct-users
Hi Jorge,

The general approach looks good. In which cases exactly do you get the
ambiguous method error? Can you share a complete example, e.g. by
forking the https://github.com/mapstruct/mapstruct-examples repo and
taking one of the examples as starting point?

Thanks,

--Gunnar
Reply all
Reply to author
Forward
0 new messages