Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix ambiguous mapping methods

I'm doing an experiment with jHipster.

I have created two entities A and B backed by DTO (mapstruct). There is a many-to-many relationship between them. They both also have a many-to-one relationship with the user.

Up until creating the last relationship, everything works fine. After creating the last many-to-one relationship, I get the following error:

[INFO] --- maven-processor-plugin:2.2.4:process (process) @ m2m ---
[ERROR] diagnostic: /Users/andy/jhipster-m2m/src/main/java/com/m2m/web/rest/mapper/AMapper.java:18: error: Ambiguous mapping methods found for mapping property "java.lang.Long userId" to com.m2m.domain.User: com.m2m.domain.User userFromId(java.lang.Long id), com.m2m.domain.User com.m2m.web.rest.mapper.BMapper.userFromId(java.lang.Long id).
A aDTOToA(ADTO aDTO);
  ^
[ERROR] error on execute: error during compilation

The definitions are very straightforward: For A:

{
  "relationships": [
    {
        "relationshipId": 1,
        "relationshipName": "b",
        "otherEntityName": "b",
        "relationshipType": "many-to-many",
        "otherEntityField": "id",
        "ownerSide": true
    },
    {
        "relationshipId": 2,
        "relationshipName": "user",
        "otherEntityName": "user",
        "relationshipType": "many-to-one",
        "otherEntityField": "id"
    }
],
"fields": [
    {
        "fieldId": 1,
        "fieldName": "nameA",
        "fieldType": "String"
    }
],
"changelogDate": "20150909165353",
"dto": "mapstruct",
"pagination": "no"

}

For B:

{
"relationships": [
    {
        "relationshipId": 1,
        "relationshipName": "a",
        "otherEntityName": "a",
        "relationshipType": "many-to-many",
        "ownerSide": false,
        "otherEntityRelationshipName": "b"
    },
    {
        "relationshipId": 2,
        "relationshipName": "user",
        "otherEntityName": "user",
        "relationshipType": "many-to-one",
        "otherEntityField": "id"
    }
],
"fields": [
    {
        "fieldId": 1,
        "fieldName": "nameB",
        "fieldType": "String"
    }
],
"changelogDate": "20150909165433",
"dto": "mapstruct",
"pagination": "no"

}

I'm really stuck on this. Any help is very much appreciated!!

EDIT: Providing github repo that demonstrates the problem https://github.com/andyverbunt/jhipster-m2m.git

like image 693
Andy Verbunt Avatar asked Nov 19 '25 05:11

Andy Verbunt


2 Answers

This seems like a bug as mentioned in comment above. For time being you can either remove the method from one of the mapper or rename the method in any one mapper, we will need to look into how to avoid this during generation in Jhipster

This also can be fixed by using MapStruct qualifiers (see Selection based on Qualifiers in the reference documentation).

like image 174
Deepu Avatar answered Nov 21 '25 19:11

Deepu


This seems to be a bug, but not like mentioned above. The JHipster mapper generator doesn't correctly add @Mapper(..., users = {UserMapper.class}) to the Mapper classes. I'm answering this a year later because this is still the case with JHipster 3.12.2.

In the source you shared replace the following line:

@Mapper(componentModel = "spring", uses = {BMapper.class, })

With this:

@Mapper(componentModel = "spring", uses = {BMapper.class, UserMapper.class})
like image 41
stilgar Avatar answered Nov 21 '25 20:11

stilgar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!