by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Tobias Liese
    @SR-Lut3t1um

    you probably are doing a PATCH operation with a collection, right?

    Yes I do

    ER-GAIBI
    @ER-GAIBI
    Hello, is there a way to ignore mapping all fileds except some specific fields ?
    ER-GAIBI
    @ER-GAIBI
    I have to ignore many fields, should I put ignore = true for all fileds ? or there is an optimal way ?
    ER-GAIBI
    @ER-GAIBI
    I found this solution : @BeanMapping(ignoreByDefault = true) @Mapping(target = "id", source = "id")
    Filip Hrisafov
    @filiphr
    Yes that is the solution that you can use
    @BeanMapping(ignoreByDefault = true)
    ER-GAIBI
    @ER-GAIBI
    Thanks
    Eirches
    @Eirches

    Hello - I have a project where I am using MapStruct and Lombok together. So far the tool has been very powerful, but I think I have an odd question:

    Is it possible to have the mapper return the builder itself rather than the result of .build()?

    Filip Hrisafov
    @filiphr
    Yes the mapper can return the builder. You need to define the return type of your method to be the bulder
    Eirches
    @Eirches
    @filiphr I did attempt that, but Lombok makes the constructor of the builder private and exposes access to an instance through SomeClass.builder().
    MapStruct is capable of recognizing this when attempting to map to SomeClass, but when mapping to SomeClass.SomeClassBuilder the generated code attempts to create one directly resulting in an illegal access compilation failure.
    Filip Hrisafov
    @filiphr
    You can always create a method with @ObjectFactory to provide an instance of the builder
    Eirches
    @Eirches
    Side note - It appears that you also have to use Lombok's withPrefix = "set" on the builder in order to generate the correct mapping code. This is problematic because of an oversight in the Lombok project where they did not include this setting on the SuperBuilder annotation, and so it isn't a viable solution with objects using inheritance. Fortunately a PR is already merged and that will be addressed in the next release.
    I'll give the ObjectFactory another shot. I'm guessing a may have misunderstood that annotation and it would fix my problem.
    Filip Hrisafov
    @filiphr
    It should look something like
    @ObjectFactory
    default SomeClass.SomeClassBuilder createBuilder() {
        return SomeClass.builder();
    }
    Eirches
    @Eirches
    That looks like it works, thank you.
    ER-GAIBI
    @ER-GAIBI
    @Mapping(target = "updateDate", expression = "java( transformUpdateDate(operation))")
        @Mapping(target = "simpleVarsRemaining", ignore = true)
        @Mapping(target = "operationEmmyListConditions", ignore = true)
        @Mapping(target = "ficheEmmiesRemaining", ignore = true)
        @Mapping(target = "creationDate", ignore = true)
        @Mapping(target = "editionDate", ignore = true)
        @Mapping(target = "ficheCalcul", ignore = true)
        @Mapping(target = "institution", ignore = true)
        @Mapping(target = "soustraitant", ignore = true)
        @Mapping(target = "inFolder", ignore = true)
        @Mapping(target = "isCpe", ignore = true)
        @Mapping(target = "cpe", ignore = true)
        @Mapping(target = "forfait", ignore = true)
        @Mapping(target = "precariteValue", ignore = true)
        @Mapping(target = "operationVolume", expression = "java(transformOperationVolume(operation.getOperationVolume()))")
        @Mapping(target = "installateur", expression = "java( transformInstallateur(operation.getInstallateur()))")
        @Mapping(target = "verificateur1", expression = "java( transformVerificateur(operation.getVerificateur1()))")
        @Mapping(target = "verificateur2", expression = "java( transformVerificateur(operation.getVerificateur2()))")
        @Mapping(target = "verificateur3", expression = "java( transformVerificateur(operation.getVerificateur3()))")
        @Mapping(target = "opportunity", expression = "java( transformOpportunity(operation.getOpportunity()))" )
        @Mapping(target = "fiche", expression = "java( operation.getFicheCalcul() != null ? " +
                "transformFiche(operation.getFicheCalcul().getFiche()) : null)")
        @Mapping(target = "instutitionName", expression = "java( operation.getInstitution() != null ? " +
                "operation.getInstitution().getSiteName() : null)")
        @Mapping(target = "daysSinceCreation", expression = "java(calculateDaysSInceCreation(operation.getCreationDate()))")
        OperationDTO toDisplayOperationDTO(Operation operation);

    I metionned that mapstruct should map the field daysSinceCreation with the expression calculateDaysSInceCreation(Long creationDate)

    Why mapstruct maps all field of type Long such as the ID field

    default Long calculateDaysSInceCreation(Long creationDate){
            if (creationDate != null) {
                LocalDateTime dateOfCreation = LocalDateTime.ofInstant(Instant.ofEpochMilli(creationDate),
                        TimeZone.getDefault().toZoneId());
                LocalDateTime today = LocalDateTime.now();
                return Duration.between(dateOfCreation, today).toDays();
            } else {
                return null;
            }
        }

    Here is the implementation :

    @Override
        public OperationDTO toDisplayOperationDTO(Operation operation) {
            if ( operation == null ) {
                return null;
            }
    
            OperationDTO operationDTO = new OperationDTO();
    
            operationDTO.setId( calculateDaysSInceCreation( operation.getId() ) );
            operationDTO.setBillRef( operation.getBillRef() );
            operationDTO.setCanRegroup( operation.isCanRegroup() );
            operationDTO.setBeginDate( calculateDaysSInceCreation( operation.getBeginDate() ) );
            operationDTO.setEndDate( calculateDaysSInceCreation( operation.getEndDate() ) );
            operationDTO.setZni( operation.isZni() );
            operationDTO.setStatut( operation.getStatut() );
            if ( operation.getNatureDuRole() != null ) {
                operationDTO.setNatureDuRole( operation.getNatureDuRole().name() );
            }
            operationDTO.setBeneficiary( beneficiaryToBeneficiaryDto( operation.getBeneficiary() ) );
            operationDTO.setLot( operation.getLot() );
            operationDTO.setComment( operation.getComment() );
            operationDTO.setVerificationStatut( operation.getVerificationStatut() );
            operationDTO.setDeadlineForFiling( calculateDaysSInceCreation( operation.getDeadlineForFiling() ) );
            .....
            return operationDto;
    }
    As you can see, mapstruct use the calculateDaysSInceCreation(Long l) to map the id, begindate, enddate fields
    ER-GAIBI
    @ER-GAIBI

    I changed the calculateDaysSInceCreation(Long l) to calculateDaysSInceCreation(Operation op), and it works good

    default Long calculateDaysSInceCreation(Operation operation){
            if (operation.getCreationDate() != null) {
                LocalDateTime dateOfCreation = LocalDateTime.ofInstant(Instant.ofEpochMilli(operation.getCreationDate()),
                        TimeZone.getDefault().toZoneId());
                LocalDateTime today = LocalDateTime.now();
                return Duration.between(dateOfCreation, today).toDays();
            } else {
                return null;
            }
        }

    Can any one explain this pls ?

    Filip Hrisafov
    @filiphr
    If MapStruct finds a method between types that it can use it will use it. You by defining Long calculateDaysSInceCreation(Long) you are telling MapStruct use this method for mapping between longs
    You need to look at qualifiers
    btw all your mappings can be done without the needed of using an expression
    ER-GAIBI
    @ER-GAIBI
    Thank you for your explinaintion, yes I agree that all mappings can be done without using expression
    berryh
    @berryh
    Hello, I am trying out the new beta version of MapStruct (1.4.0.Beta2) and I'm running into what seems like a bug to me. I am using an interface which serves as a base definition for converting types and their list forms using generics. This used to work in MapStruct 1.3.1.Final, but doesn't compile in neither of the new beta versions. Can someone help me with this issue? Is there a change I should know about for example? I created a simple project to showcase this: https://github.com/berryh/Mapstruct140Beta2ListMapperInterfaceTest
    santhosh117
    @santhosh117
    Hi all, I have nested DTO need to use mapstruct to field mapping
    can one help to do
    Filip Hrisafov
    @filiphr
    @berryh that should work looks pretty simple to me. Can you please raise and issue with the link to the project. It would be helpful to track it
    @santhosh117 you'll need to provide more information
    berryh
    @berryh
    @filiphr That's what I thought, but I wanted to make sure. I have created an issue: mapstruct/mapstruct#2153
    Filip Hrisafov
    @filiphr
    Thanks
    pranayd2693
    @pranayd2693
    Hi @filiphr , would like to know if mapstruct supports object to map mapping?
    If it supports, someone can help with sample example?
    Filip Hrisafov
    @filiphr
    Not yet. Have a look at mapstruct/mapstruct#1075
    sh|Ook!
    @SmashHouseOok_twitter
    Hello! is it possible to use mapstruct to convert an array into members?
    e.g, int numbers[] into number1 number2
    @Mapping(target="number1, number2", source="numbers")
    1 reply
    TejasBhanushali
    @TejasBhanushali

    Hello Team,

    Im using , CDI, JDK7 and wildfy 8.2 and Mapstruct 1.3.1 in my project where I'm getting below exception.

    org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type SellerTypeEntityToSellerMapper with qualifiers @Labeled
    at injection point [BackedAnnotatedField] @Inject @Labeled com.atulsia.instaseller.daoImpl.SellerDaoImpl.entityToSellerMapper
    at com.atulsia.instaseller.daoImpl.SellerDaoImpl.entityToSellerMapper(SellerDaoImpl.java:0)

    This is my mapper class:
    @Mapper(componentModel = "cdi",injectionStrategy = InjectionStrategy.FIELD)
    @Labeled("sellerEntityMapper")
    public interface SellerTypeEntityToSellerMapper {

    Seller sellerTypeEntityToSeller(SellerTypeEntity sellerTypeEntity);

    }

    Im using in RestService by @Inject:

    1 reply
    @Inject @Labeled("sellerEntityMapper") SellerTypeEntityToSellerMapper entityToSellerMapper;
    Note: Labeled is our custome anotation
    Please help me as im facing this issue since last 2 days.
    uniQ
    @aniq-pirzada
    Hi,
    Is it possible to provide a @Mapping(target = "A", source="sourceA, SourceB", qualifiedByName= "mapToA")
    my mapToA function takes in 3 variables but i do not know how to pass them through using @Mapping
    1 reply
    samilazrak
    @samilazrak

    Hi,

    I'm using in my SpringBoot project lombok annotations (version 1.18.10), and I am trying to make a proof of concept of MapStruct.
    We use Lombok annotation @RequiredArgsConstructor(onConstructor = @_ _(@Inject)) for Dependency Injection (which is a mandatory requirement of the project).

    But when I update my pom.xml to add MapStruct dependency, I get the following error when trying to run my code :

    java : cannot find symbol
    symbol : class

    It works well when I use Spring field Injection @Inject but not with the Constructor Injection using Lombok's annotation.

    Please could you help me figure out why.

    Filip Hrisafov
    @filiphr
    @samilazrak which symbol cannot be found? How does the generated code look like? The Lombok annotation has nothing to do with the generated mappers
    @manav-bhanot please look at my comment on your question. Don't use single letters for classes and fields. It is much easier to help when clear objects with names that we can relate to is used
    Manav Bhanot
    @manav-bhanot
    @filiphr Thank you for your response. I've updated the code.
    lw-mcno
    @lw-mcno

    Hello, I'm using CDI constructor-based injection and have an interface SomeMapper which uses = {DynamicMapper.class} - an another @ApplicationScoped bean providing some dynamic mapping methods, looking the data up in the "DB".

    Would it be possible to "inject" a DynamicMapper instance into a default mapper-interface method without converting the mapper to an abstract class (I don't want to lose the composability of interfaces) and using constructor-injection there?

    In other words, is it possible to access some method contained in DynamicMapper from SomeMapper? Passing it an method argument doesn't work and/or annotating @Context and @ObjectFactory don't seem appropriate here.

    2 replies
    sutapadey
    @sutapadey
    Hello, I'm using a older version of mapstruct 1.0.0.final with spring and jdk7. We write unit tests using jmockit. Is there a way to get/generate the impl class for mapstruct mappers in test scope?
    6 replies
    Al Grant
    @bigal_nz_twitter
    morning all
    I have a DTO and want more than one source class
    @Mapper(componentModel = "spring", uses = {TaskMapper.class})
    public interface PlanMapper extends EntityMapper<PlanDTO, Plan> {
    
        @Mapping(target = "removeTask", ignore = true)
        Plan toEntity(PlanDTO planDTO);
    
        default Plan fromId(Long id) {
            if (id == null) {
                return null;
            }
            Plan plan = new Plan();
            plan.setId(id);
            return plan;
        }
    }
    1 reply
    how would I do this