简体   繁体   English

MapStruct spring 启动

[英]MapStruct spring boot

Does anyone know why mapStruct doesn't allow DTO class to have less elements than the ENTITY class.有谁知道为什么 mapStruct 不允许 DTO class 的元素少于 ENTITY class。

for example I have this entity:例如我有这个实体:

public class Provider {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    @OneToMany(cascade=CascadeType.ALL, mappedBy="provider")
    private Set<Product> products;

}

and the dto:和 dto:




public class ProviderDTO {

    private Long id;
    private String name;

}

the Dto doesn't contain the attribute 'products' which give me this error: Dto 不包含给我这个错误的属性“产品”: 如果出现错误,请点击以查看图像

ps: when i add List to the DTO, everything works fine. ps:当我将 List 添加到 DTO 时,一切正常。 But i want that my DTO class contains only the attributs that I want, not the same ones as in the Entity class.但我希望我的 DTO class 只包含我想要的属性,而不是实体 class 中的属性。

When your source and target objects are not identical, you need to define the difference in your mapper class's mapping method.当您的源对象和目标对象不相同时,您需要在映射器类的映射方法中定义差异。

  1. If source and target object has different names of properties you need to define which property name of source will be mapped to which property name of the target.如果源和目标 object 具有不同的属性名称,您需要定义源的哪个属性名称将映射到目标的哪个属性名称。

  2. If target does not have a property but the source has, you need to specify ignore for that property on the mapping method.如果目标没有属性但源有,则需要在映射方法上为该属性指定忽略。

    @Mapping(target = "products", ignore = true) @Mapping(目标=“产品”,忽略=真)

Looking at the error message it seems that you have the DTO objects, the entity objects and the mapper most likely each in their own modules.查看错误消息,您似乎在自己的模块中拥有 DTO 对象、实体对象和映射器。 But when adding them to the end product you are using versions that aren't matching with the functionality needed.但是当将它们添加到最终产品时,您使用的版本与所需的功能不匹配。

The generated mapper that is being used expects the List object in the DTO class. But the supplied DTO information doesn't have this.正在使用的生成的映射器需要 DTO class 中的列表 object。但是提供的 DTO 信息没有这个。 Which means that the mapper needs to be updated so that it knows that the DTO class doesn't have this field.这意味着映射器需要更新,以便它知道 DTO class 没有这个字段。

In code:在代码中:

// DTO module version 2
class DTO {
  private String field1;
  private String field2;
  // removed: private List<String> list1;
  // leaving out setters/getters
}

// Entity module version 1
class Entity {
  private String field1;
  private String field2;
  private List<String> list1;
  // leaving out setters/getters
}

// Mapper module version 1 with dependencies on:
//   DTO module version 1
//   Entity module version 1
interface Mapper {
  DTO EntitytoDto(Entity source);
}

@Generated
class MapperImpl {
  DTO EntitytoDto(Entity source) {
    // mapping code for field1, field2 and list1.
  }
}

End result of the above code is a NoSuchMethodError when it tried to map list1 .上面代码的最终结果是NoSuchMethodError当它尝试 map list1时。

What you want is:你想要的是:

// Mapper module version 2 with dependencies on:
//   DTO module version 2
//   Entity module version 1
interface Mapper {
  DTO EntitytoDto(Entity source);
}

@Generated
class MapperImpl {
  DTO EntitytoDto(Entity source) {
    // mapping code for field1 and field2.
  }
}

Hope this helps with solving the issue.希望这有助于解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM