带注释的推土机深度映射
无法将从源类到目标类 SourceEmployee.Address.houseName
的深度映射映射到 TargetEmployee.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());
}
}
Unable to map deep mapping from Source class to Target class SourceEmployee.Address.houseName
to TargetEmployee.houseName
Source object class
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;
}
}
Address class
public class Address
{
private String houseName;
public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName;
}
}
target object
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;
}
}
Main class copying from source to destination
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());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果其他人遇到同样的问题,我可以通过这样做来纠正这种情况:
SourceEmployee 中的集合
不是必需的
In case someone else run into the same problem, I manage to correct that case by doing this :
The
set in SourceEmployee Isn't necessary
在源类(SourceEmployee)中添加这个注解就足够了:
事实上,“this”的意思是“保持在同一个节点上”;之后,houseName 将在 Address 和 TargetEmployee 之间匹配。
;)
亚历克斯
it's enough to add this annotation in the source class (SourceEmployee):
In fact, "this" means "remain on the same node"; after that, houseName will match between Address and TargetEmployee.
;)
Alex