Andy Verbunt
Andy Verbunt

Reputation: 387

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

Upvotes: 3

Views: 11742

Answers (2)

stilgar
stilgar

Reputation: 101

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})

Upvotes: 2

Deepu
Deepu

Reputation: 1301

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).

Upvotes: 4

Related Questions