如何修复不明确的映射方法

How to fix ambiguous mapping methods

我正在用 jHipster 做一个实验。

我创建了两个由 DTO (mapstruct) 支持的实体 A 和 B。 它们之间是多对多的关系。 他们都与用户也有多对一的关系。

在创建最后一个关系之前,一切正常。 创建最后一个多对一关系后,出现以下错误:

[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

定义非常简单: 对于答:

{
  "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"

}

对于乙:

{
"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"

}

我真的卡在这上面了。 非常感谢任何帮助!!

编辑:提供 github 演示问题的回购 https://github.com/andyverbunt/jhipster-m2m.git

这似乎是上面评论中提到的错误。目前您可以从一个映射器中删除该方法或在任何一个映射器中重命名该方法,我们将需要研究如何在 Jhipster 生成期间避免这种情况

这也可以通过使用 MapStruct 限定符来解决(请参阅参考文档中的 Selection based on Qualifiers)。

这似乎是一个错误,但不像上面提到的那样。 JHipster 映射器生成器没有正确地将 @Mapper(..., users = {UserMapper.class}) 添加到映射器 类。我在一年后回答这个问题,因为 JHipster 3.12.2 仍然如此。

在您共享的源代码中替换以下行:

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

有了这个:

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