英文:
mapstruct, Java and Spring Issues with target is not added
问题
我确实有一个DTO类和一个类似这样的实体类。
class ADTO {
....
....
HashSet<BDTO> b;
}
class AEntity {
.....
.....
HashSet<BEntity> b;
}
class BDTO {
...
....
}
class BEntity {
.....
.....
AEntity a;
}
我尝试使用映射器和 uses。
@Mapper(componentModel = "spring", uses=BMapper.class)
interface AMapper {
ADTO toDTO(AEntity a);
AEntity fromDTO(ADTO a);
}
@Mapper(componentModel = "spring", uses=AMapper.class)
interface BMapper {
BDTO toDTO(BEntity b);
BEntity fromDTO(ADTO a, BDTO b);
}
这是用于 "一对多" 关系的 "JPA实体" 内部的。 "DTO" 被用于 JSON(Jackson解析器)。
ADTO toDTO(AEntity a);
生成器类正在使用上面的Bmapper方法。
但是
AEntity fromDTO(ADTO a);
没有使用 Bmapper。我如何强制使用 Bmapper?
英文:
I do have one DTO Class and an Entity Class like this.
class ADTO {
....
....
HashSet<BDTO> b;
}
class AEntity {
.....
......
HashSet<BEntity> b;
}
class BDTO {
...
....
}
class BEntity {
.....
......
AEntity a;
}
I tried to use mapper with uses
@Mapper(componentModel = "spring", uses=BMapper.class)
interface AMapper {
ADTO toDTO(AEntity a);
AEntity fromDTO(ADTO a);
}
@Mapper(componentModel = "spring", uses=AMapper.class)
interface BMapper {
BDTO toDTO(BEntity b);
BEntity fromDTO(ADTO a, BDTO b);
}
It is for "One to many" relations inside the "JPA entity". "DTO" is being used for JSON (Jackson parser).
ADTO toDTO(AEntity a);
The generator class is using Bmapper above method.
But
AEntity fromDTO(ADTO a);
is not using Bmapper. How can I force to use Bmapper?
答案1
得分: 0
使用@InheritInverseConfiguration
。它建议代码生成器将来自反向映射方法的所有映射应用于注释方法。
@Mapper(componentModel = "spring", uses = BMapper.class)
interface AMapper {
ADTO toDTO(AEntity a);
@InheritInverseConfiguration
AEntity fromDTO(ADTO a);
}
英文:
Try using @InheritInverseConfiguration
. It advises the code generator to apply all the Mappings from an inverse mapping method to the annotated method as well.
@Mapper(componentModel = "spring", uses=BMapper.class)
interface AMapper {
ADTO toDTO(AEntity a);
@InheritInverseConfiguration
AEntity fromDTO(ADTO a);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论