mapping an object to a list of objects using spring

976 views
Skip to first unread message

declan roche

unread,
Aug 10, 2017, 6:56:02 AM8/10/17
to mapstruct-users

Hi ,

I wonder if you can help me.

I am trying to map an object A to a list of object B

I have a mapper which maps from object A to object B

 

I Have tried a number of different ways for example

Trying to create a list with one object A using expressions

And qualifiedByName but this does not work because I think

Because when you use expressions/qualifiedByName you can not use

Custom mappers ( I could be wrong here ?)

 

I also tried to call the mapper from the @aftermapper method using

The mappers.getMapper to get a handle on the target mapper

But I found that the spring beans used in the mapper

Where not being populated. Mapping in the aftermapping makes

Sense in that I can create a list and put my object into it

Before passing to the mapper. So I am hoping that there is another

Way to get a handle on the mapper component from my mapper.

 

All my mappers use

@Mapper(componentModel="spring",

 

All suggestions are welcome

 

Regards,

Declan

 

 

// SPECIESTOLOGSPECY.JAVA

// From this mapper I want to call SpecyToLogDeclarationMapperApi mapper

// to map ‘species’ to ‘logdeclarations’ which is a list of logdeclaration

// you can see want I am trying to do in the aftermapping method

// where I map species to logdeclaration and then put this into a list

// unfortunately I need other mapping components to be autowired into

//  SpecyToLogDeclarationMapperApi and this is not happening.

// is there another way to get a handle to the spring managed

// SpecyToLogDeclarationMapperApi mapper ?

 

@Mapper(componentModel="spring",

uses = {

            ConfigMapperFromCode.class,

            GeoInfoMapper.class,

            SpecyToLogDeclarationMapperApi.class

                        })

public interface SpeciesToLogSpecy {

      SpecyToLogDeclarationMapperApi MAPPER = Mappers.getMapper(SpecyToLogDeclarationMapperApi.class);

     

      @Mappings(

                  {

                         @Mapping(target="createdate", expression = "java(java.sql.Timestamp.valueOf(java.time.LocalDateTime.now()))"),

                         @Mapping(target="speciesid", qualifiedByName={"ConfigMapperFromCode", "speciesIdFromCodeAsDecimal"},  source = "species.speciesCode"),

                         @Mapping(target="unitweight", constant = "1"),

                         @Mapping(target = "inactiveind", constant = "N"),

                         @Mapping(target = "unitdefaultind", constant = "Y"),

                         @Mapping(target = "sectiontypeid", expression = "java(new BigDecimal(ie.gov.agriculture.fisheries.logsheet.mapping.constants.MappingConstants.LOG_SPECIES_SECTION_TYPE_SHEETDECLARATION))"),

                         @Mapping(target = "unituomid", expression = "java(new BigDecimal(ie.gov.agriculture.fisheries.logsheet.mapping.constants.MappingConstants.LOGSHEET_CATCHUNITS_KG))"),

                         @Mapping(target="catchtypeid",  qualifiedByName={"ConfigMapperFromCode", "returnCatchTypeId"},  source = "species.spType"),

                         @Mapping(target="legalfishsizetypeid",  qualifiedByName={"ConfigMapperFromCode", "legalFishSizeTypeIdFromCode"},  source = "species.fishSizeClass"),

                         @Mapping(target="presenttypeid",  qualifiedByName={"ConfigMapperFromCode", "presentationTypeIdFromCode"},  source = "species.presentation.presentationType"),

                         //@Mapping(target="logdeclarations", source = "species")                       

                  }                

                   )

      Logspecy speciesToLogspecy(Species species, @Context ExtraFields extraFields);

 

 

@AfterMapping

default void afterMap(@MappingTarget Logspecy logspecy, @Context ExtraFields extraFields){

Logdeclaration logDeclaration = MAPPER.SpeciesToLogDeclarations(species, extraFields);

                 

      List<Logdeclaration> logdeclarations = new ArrayList<Logdeclaration>();

      logdeclarations.add(logDeclaration);

      logSpecy.setLogdeclarations(logdeclarations);   

{

 

 

// SPECYTOLOGDECLARATIONMAPPERAPI.JAVA

 

 

 

@Mapper(componentModel="spring",

uses = {

            ConfigMapperFromCode.class,

            SpecyToFishDeclarationMapperApi.class           

            },

      unmappedTargetPolicy = ReportingPolicy.IGNORE,

      nullValueCheckStrategy =  NullValueCheckStrategy.ALWAYS

)

public interface SpecyToLogDeclarationMapperApi {

     

     

      @Mappings(

                  {

                         @Mapping(target="createdate", expression = "java(java.sql.Timestamp.valueOf(java.time.LocalDateTime.now()))"),                  

                         @Mapping(target="geartypeid", qualifiedByName={"ConfigMapperFromCode", "gearIdFromCode"},  source = "species.gearType"),

                         @Mapping(target="fishcount", source = "species.qty"),

                         @Mapping(target = "inactiveind", constant = "N"),

                         @Mapping(target="packagetypeid", qualifiedByName={"ConfigMapperFromCode", "packagingTypeIdFromCode"},  source = "species.presentation.packaging"),

                         @Mapping(target="packagecount", source = "species.presentation.pkgunit"),

                         @Mapping(target="avgpackageweight", source = "species.presentation.pkgUnitWeight"),

                         @Mapping(target="conversionfactor", source = "species.presentation.convFactor"),

                         @Mapping(target="fishdeclaration", source = "species.geoInfo")                       

                  }                

                  )

      Logdeclaration SpeciesToLogDeclarations (Species species, @Context ExtraFields extraFields);     

 

 

 

 

Filip Hrisafov

unread,
Aug 12, 2017, 2:59:37 AM8/12/17
to mapstruct-users
Hey Declan,

I've answered your question on StackOverflow. here is my answer.

In a nutshell, you are missing a method that would map from Species to List<Logdeclaration>

Cheers,
Filip

declan roche

unread,
Aug 14, 2017, 4:08:02 AM8/14/17
to mapstruct-users


Thanks Alot Filip thanks for your help
Reply all
Reply to author
Forward
0 new messages