2015-09-09 66 views
2

我正在做一个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" 

}

对于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" 

}

我真的被困在这。 任何帮助非常感谢!

编辑:演示该问题https://github.com/andyverbunt/jhipster-m2m.git

+0

为什么你有映射两种方法从ID用户?他们有什么不同? – Gunnar

+0

嗨Gunnar,感谢您的反馈。 AFAIK,没有两种方法将id映射到用户,至少不在同一个对象中。实体A需要知道它的所有者,B也是一样,但它们是不同的对象。 我会在github上发布示例并编辑帖子,以便友好的助手可以看看它。 –

+0

您是否手动更改了任何内容?如果看起来不像我们使用mapstruct生成的bug。当mapstruct试图将userID从DTO映射到User中的id时,它看到两个方法'userFromId'正在执行该功能,一个在AMapper中,另一个在BMapper中 – Deepu

回答

3

提供了GitHub库这似乎是在评论上述的错误。暂时你可以从一个mapper中移除该方法或者在任何一个mapper中重命名该方法,我们需要研究如何在Jhipster中生成这个方法

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

+0

嗨Deepu,希望我更好地了解maptruct的工作原理。我尝试重命名一些方法,但到目前为止我没有任何运气。你会介意在回购中做出这样的拉动请求 - 当然你有时间... https://github.com/andyverbunt/jhipster-m2m 我需要在A和B,所以我想重命名是唯一的选择,不是吗? –

+0

嗨Deepu, 做了一些实验... 更改方法名称不会改变任何东西。最后它仍然是相同的签名,所以它仍然不明确。 删除方法确实有效。他们都做同样的事情,所以你删除哪一个并不重要。 根据你的建议,我会进一步研究部分限定词。它绝对听起来像一个更干净的解决方案。我希望这会让jHipster 2.21变得更好,因为我相信很多人都受到这个bug的影响。 我想我们应该把bugreport打开,不是吗? 感谢您的帮助:) –

+0

雅请将它打开 – Deepu

0

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

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

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

有了这个:

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