mapstruct, Java 和 Spring 的问题:目标未添加

huangapple go评论65阅读模式
英文:

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&lt;BDTO&gt; b; 
}

class AEntity {
.....
......
   HashSet&lt;BEntity&gt; b; 
}

class BDTO {
...
....
    
}

class BEntity {
.....
......
   AEntity a; 
}

I tried to use mapper with uses

@Mapper(componentModel = &quot;spring&quot;, uses=BMapper.class)
interface AMapper {
    
  ADTO toDTO(AEntity a);
   
  AEntity fromDTO(ADTO a);  
} 

@Mapper(componentModel = &quot;spring&quot;, 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 = &quot;spring&quot;, uses=BMapper.class)
interface AMapper {
    
  ADTO toDTO(AEntity a);

  @InheritInverseConfiguration 
  AEntity fromDTO(ADTO a);  
}

huangapple
  • 本文由 发表于 2020年8月8日 11:12:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63311457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定