英文:
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 = "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 and getters omitted
}
Here is the ModelMapper configuration.
@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);
}
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<PlanDTO, Plan>() {
@Override
protected void configure() {
map().getId().setName(source.getName());
}
});
All the other fields should be implicitly mapped by their name. Even the PlanId.id
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论