[英]How to instantiate a subclass with Dozer?
I have following target classes: 我有以下目标课程:
public class Person {
private String firstName;
private String lastName;
...
}
public class Employee extends Person {
private String postion;
...
}
public class PersonContainer {
private Person person;
...
}
And this is my source: 这是我的来源:
public class Form {
private String firstNameEmployee;
private String lastNameEmployee;
private String positionEmployee;
...
}
I would like to get object PersonContainer but not with Person object but with Employee object. 我想获取对象PersonContainer,但不获取Person对象,而是获取Employee对象。 I really have no idea how to achieve that. 我真的不知道该如何实现。 How to tell Dozer to instantiate a subclass? 如何告诉Dozer实例化一个子类?
This mapping gives Person object: 此映射提供了Person对象:
<mapping>
<class-a>hl.test.dozer03.form.Form</class-a>
<class-b>hl.test.dozer03.result.PersonContainer</class-b>
<field>
<a>firstNameEmployee</a>
<b>person.firstName</b>
</field>
<field>
<a>lastNameEmployee</a>
<b>person.lastName</b>
</field>
</mapping>
Can this be done by slightly modifying this mapping ? 可以通过稍微修改此映射来完成此操作吗?
You need to use a custom-createMethod. 您需要使用custom-createMethod。 http://dozer.sourceforge.net/documentation/customCreateMethod.html http://dozer.sourceforge.net/documentation/customCreateMethod.html
Something like this: 像这样:
<mapping>
<class-a>hl.test.dozer03.form.Form</class-a>
<class-b create- method="PersonContainerFactory.createPersonContainer">hl.test.dozer03.result.PersonContainer</class-b>
<field>
<a>firstNameEmployee</a>
<b>person.firstName</b>
</field>
<field>
<a>lastNameEmployee</a>
<b>person.lastName</b>
</field>
</mapping>
With the java class: 使用java类:
public class PersonContainerFactory {
public static PersonContainer createPersonContainer(){
PersonContainer cont = new PersonContainer();
cont.setPerson(new Employee());
return classA;
}
}
Try changing the Person class fields to protected so they can be inherited 尝试将Person类字段更改为protected,以便可以继承它们
public class Person {
protected String firstName;
protected String lastName;
...
}
Change from PersonContainer to (what it is you want) 从PersonContainer更改为(您想要的)
public class EmployeeContainer {
private Employee employee;
...
}
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.