Java ModelMapper:将DTO映射到EmbeddedId实体类

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

Java ModelMapper: map DTO to EmbeddedId entity class

问题

这里是您提供的代码部分翻译:

PlanDTO

  1. public class PlanDTO implements Serializable {
  2. private static final long serialVersionUID = 1L;
  3. private Long id;
  4. private String name;
  5. private String formula;
  6. private String frequency;
  7. private String pricingtable;
  8. // 省略了 getters 和 setters
  9. }

PlanId

  1. @Embeddable
  2. public class PlanId implements Serializable {
  3. private static final long serialVersionUID = 1L;
  4. private Long id;
  5. private String name;
  6. public PlanId() { }
  7. public PlanId(Long id, String name) {
  8. this.id = id;
  9. this.name = name;
  10. }
  11. // 省略了 getters 和 setters
  12. }

Plan

  1. @Entity
  2. @Table(name = "plan")
  3. public class Plan implements Serializable {
  4. private static final long serialVersionUID = 1L;
  5. @EmbeddedId
  6. private PlanId id;
  7. @Column(name = "formula")
  8. private String formula;
  9. @Column(name = "frequency")
  10. private String frequency;
  11. @Column(name = "pricingtable")
  12. private String pricingTable;
  13. public Plan() { }
  14. // 省略了 setters 和 getters
  15. }

以下是 ModelMapper 的配置:

  1. @Bean
  2. public ModelMapper modelMapper() {
  3. ModelMapper modelMapper = new ModelMapper();
  4. modelMapper.getConfiguration().setAmbiguityIgnored(true);
  5. PropertyMap<PlanDTO, Plan> itemMap1 = new PropertyMap<PlanDTO, Plan>() {
  6. protected void configure() {
  7. map().setFormula(source.getFormula());
  8. map().setFrequency(source.getFrequency());
  9. map().setId(new Plan(source.getId(), source.getName()));
  10. map().setPricingTable(source.getPricingtable());
  11. }
  12. };
  13. modelMapper.addMappings(itemMap1);
  14. }

但在运行时发生了如图所示的问题:调试图像

配置有问题吗?是不是漏掉了什么?

英文:

I want to know if it's possible to map a DTO to an entity class with a composite pk. I've been reading ModelMapper documentation about PropertyMap but I can't make it work.

Here is the code:

PlanDTO:

  1. public class PlanDTO implements Serializable {
  2. private static final long serialVersionUID = 1L;
  3. private Long id;
  4. private String name;
  5. private String formula;
  6. private String frequency;
  7. private String pricingtable;
  8. // getters and setters omitted

PlanId:

  1. @Embeddable
  2. public class PlanId implements Serializable {
  3. private static final long serialVersionUID = 1L;
  4. private Long id;
  5. private String name;
  6. public BillingPlanId() { }
  7. public PlanId(Long id, String name) {
  8. this.id = id;
  9. this.name = name;
  10. }
  11. // getters and setters omitted
  12. }

Plan:

  1. @Entity
  2. @Table(name = &quot;plan&quot;)
  3. public class Plan implements Serializable {
  4. private static final long serialVersionUID = 1L;
  5. @EmbeddedId
  6. private PlanId id;
  7. @Column(name = &quot;formula&quot;)
  8. private String formula;
  9. @Column(name = &quot;frequency&quot;)
  10. private String frequency;
  11. @Column(name = &quot;pricingtable&quot;)
  12. private String pricingTable;
  13. public Plan() { }
  14. //setters and getters omitted
  15. }

Here is the ModelMapper configuration.

  1. @Bean
  2. public ModelMapper modelMapper() {
  3. ModelMapper modelMapper = new ModelMapper();
  4. modelMapper.getConfiguration().setAmbiguityIgnored(true);
  5. PropertyMap&lt;PlanDTO, Plan&gt; itemMap1 = new PropertyMap&lt;PlanDTO, Plan&gt;() {
  6. protected void configure() {
  7. map().setFormula(source.getFormula());
  8. map().setFrequency(source.getFrequency());
  9. map().setId(new Plan(source.getId(), source.getName()));
  10. map().setPricingTable(source.getPricingtable());
  11. }
  12. };
  13. modelMapper.addMappings(itemMap1);
  14. }

But this happens at runtime debug image

Is there something wrong with the configuration? Do I miss something?

答案1

得分: 2

我不太确定你的问题是什么,但是使用只有一个属性映射的映射应该很容易:

  1. modelMapper.addMappings(new PropertyMap<PlanDTO, Plan>() {
  2. @Override
  3. protected void configure() {
  4. map().getId().setName(source.getName());
  5. }
  6. });

所有其他字段都应该根据它们的名称隐式映射。甚至是 PlanId.id

英文:

I am not quite sure what is your problem but mapping should be quite easy with a just one property mapping:

  1. modelMapper.addMappings(new PropertyMap&lt;PlanDTO, Plan&gt;() {
  2. @Override
  3. protected void configure() {
  4. map().getId().setName(source.getName());
  5. }
  6. });

All the other fields should be implicitly mapped by their name. Even the PlanId.id.

huangapple
  • 本文由 发表于 2020年10月15日 00:10:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64357392.html
匿名

发表评论

匿名网友

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

确定