gpt4 book ai didi

java - 带注释的推土机深度映射

转载 作者:搜寻专家 更新时间:2023-11-01 02:31:29 25 4
gpt4 key购买 nike

无法将深度映射从源类映射到目标类 SourceEmployee.Address.houseNameTargetEmployee.houseName

源对象类

public class SourceEmployee 
{
private String empName;
private Address addr;


enter code here

@Mapping("empName")
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Address getAddr() {
return addr;
}

public void setAddr(Address addr) {
this.addr = addr;
}
}

地址类

public class Address 
{
private String houseName;
public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName;
}
}

目标对象

public class TargetEmployee 
{

private String empName;
private String houseName;

public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}

主类从源复制到目标

public class CopyAttributes 
{

public static void main(String args[])
{
SourceEmployee sourceEmp = new SourceEmployee();
sourceEmp.setEmpName("Rafi");

Address adr = new Address();
adr.setHouseName("Aashiyana");
sourceEmp.setAddr(adr);

Mapper mapper = new DozerBeanMapper();
TargetEmployee destObject = mapper.map(sourceEmp, TargetEmployee.class);

System.out.println(destObject.getEmpName());
System.out.println(destObject.getHouseName());

}
}

最佳答案

万一其他人遇到同样的问题,我设法通过这样做来纠正这种情况:

public class TargetEmployee 
{

private String empName;
private String houseName;

@Mapping("addr.houseName")
public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}

@Mapping("empName")

不需要在 SourceEmployee 中设置

关于java - 带注释的推土机深度映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8360623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com