简体   繁体   中英

Mapstruct: Ambiguous mapping methods found for mapping property

I am getting this strange error (Using IntelliJ IDEA 2022.2.2 Ultimate):

Ambiguous mapping methods found for mapping property "java.lang.Short TypeA" to TypeB: TypeB TypeBRepo.getOne(java.lang.Short id), TypeB TypeBRepo.getById(java.lang.Short id), TypeB TypeBRepogetReferenceById(java.lang.Short id)

This happened after I upgraded the spring boot version from 2.1.0.RELEASE to 2.7.2 and maven version from 3.0.0 to 3.8.6 .

TypeB Repository extends JPARepository

@Repository
public interface TypeBRepo extends JpaRepository<TypeB, Short> {
}

Mapping is done using mapstruct version 1.3.1.Final (Tried with latest mapstruct version also no change)

Following is the mapping

@Mappings({
    @Mapping(target = "TypeB", source = "request.TypeA"),
    @Mapping(target = "securityValue", source = "request.currentPrice"),
    @Mapping(target = "user", source = "user"),
    @Mapping(target = "id", ignore = true),
    @Mapping(target = "documents", ignore = true),
})
public abstract void sourceToDestination(final "Type" request,
                                         final "Type" user,
                                         final @MappingTarget TargetA input);

And the TargetA class is as follows.

    @Entity
    @Getter
    @Setter
    @DiscriminatorValue("6")
    @FieldDefaults(level = AccessLevel.PRIVATE)
    public class TargetA extends BlaBla {
    
        @NotNull
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "investment_loan_type_id")
        TypeB type_b_value;
    }

Please check if in your Controller layer you have multiple methods mapped to the same endpoint (@Path).

I would assume that in your mapper you have @Mapper(uses = TypeBRepo.class)

When that is the case MapStruct considers all methods that are available in the TypeBRepo (and its parents) as potential mapping methods. In your mapping you have a mapping from Short into TypeB .

With the upgrade to Spring Boot 2.7.2 you also did an upgrade of Spring Data. It seems like there are new methods added in Spring Data that can be used to map from Short to TypeB and thus MapStruct is no longer possible to determine which one needs to be used.

You'll need to instruct MapStruct which method to use for the mapping.

One approach would be to override the methods that you do not want MapStruct to use and annotate them with @org.mapstruct.Named . Another approach would be to create a dedicated class for mapping from Short to TypeB and use that in Mapper#uses .

You can read more about How to solve ambiguous methods in the MapStruct FAQ.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM