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

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

Java ModelMapper: map DTO to EmbeddedId entity class

问题

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

PlanDTO

public class PlanDTO implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String name;
    private String formula;
    private String frequency;
    private String pricingtable;
    // 省略了 getters 和 setters
}

PlanId

@Embeddable
public class PlanId implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String name;
    public PlanId() { }
    public PlanId(Long id, String name) {
        this.id = id;
        this.name = name;
    }
    // 省略了 getters 和 setters
}

Plan

@Entity
@Table(name = "plan")
public class Plan implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    private PlanId id;
    @Column(name = "formula")
    private String formula;
    @Column(name = "frequency")
    private String frequency;
    @Column(name = "pricingtable")
    private String pricingTable;

    public Plan() { }
    // 省略了 setters 和 getters
}

以下是 ModelMapper 的配置:

@Bean
public ModelMapper modelMapper() {
    ModelMapper modelMapper = new ModelMapper();
    modelMapper.getConfiguration().setAmbiguityIgnored(true);

    PropertyMap<PlanDTO, Plan> itemMap1 = new PropertyMap<PlanDTO, Plan>() {
        protected void configure() {
            map().setFormula(source.getFormula());
            map().setFrequency(source.getFrequency());
            map().setId(new Plan(source.getId(), source.getName()));
            map().setPricingTable(source.getPricingtable());
        }
    };
    modelMapper.addMappings(itemMap1);
}

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

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

英文:

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:

public class PlanDTO implements Serializable {
    private static final long serialVersionUID = 1L;
	private Long id;
	private String name;
	private String formula;
	private String frequency;
	private String pricingtable;
    // getters and setters omitted

PlanId:

@Embeddable
public class PlanId implements Serializable {
	private static final long serialVersionUID = 1L;
	private Long id;
	private String name;
	public BillingPlanId() { }
	public PlanId(Long id, String name) {
		this.id = id;
    	this.name = name;
    }
    // getters and setters omitted
}

Plan:

@Entity
@Table(name = &quot;plan&quot;)
public class Plan implements Serializable {
    private static final long serialVersionUID = 1L;
	@EmbeddedId
	private PlanId id;
	@Column(name = &quot;formula&quot;)
    private String formula;
 	@Column(name = &quot;frequency&quot;)
    private String frequency;
	@Column(name = &quot;pricingtable&quot;)
    private String pricingTable;

	public Plan() { }

    //setters and getters omitted
}

Here is the ModelMapper configuration.

@Bean
public ModelMapper modelMapper() {
	ModelMapper modelMapper = new ModelMapper();
	modelMapper.getConfiguration().setAmbiguityIgnored(true);

    PropertyMap&lt;PlanDTO, Plan&gt; itemMap1 = new PropertyMap&lt;PlanDTO, Plan&gt;() {
		protected void configure() {
			map().setFormula(source.getFormula());
			map().setFrequency(source.getFrequency());
			map().setId(new Plan(source.getId(), source.getName()));
			map().setPricingTable(source.getPricingtable());
		}
	};
	modelMapper.addMappings(itemMap1);
}

But this happens at runtime debug image

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

答案1

得分: 2

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

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

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

英文:

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

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

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:

确定