问题
I have the following domain structure:
abstract class Person { String name; //with getter and setter }
class Employer extends Person {}
class Employee extends Person {}
class Contract { Person contractor; //with getter and setter }
class PersonDTO implements Serializable { String name; String type; //type == 'Employee' or 'Employer' }
class ContractDTO implements Serializable { PersonDTO contractor; }
Now when I set up this following dozer mapping:
<mapping>
<class-a>Person</class-a>
<class-b>PersonDTO</class-b>
</mapping>
<mapping>
<class-a>Employer</class-a>
<class-b>PersonDTO</class-b>
</mapping>
<mapping>
<class-a>Contract</class-a>
<class-b>ContractDTO</class-b>
</mapping>
My problem concerns the mapping of the field Contract.contractor from B to A because the field Contract.contractor is an abstract class and dozer cannot guess how to instanciate it.
So my question is simple: how can I indicate to dozer that, for the mapping of the field Contract.contractor, it should instantiate an instance of Employer
if type == 'Employer
' and elsewhere Employee
?
Thank you for your help.
回答1:
You can do this with hints. Somewhat like this:
<mapping>
<class-a>Contract</class-a>
<class-b>ContractDTO</class-b>
<field>
<a>contractor</a>
<b>contractor</b>
<a-hint>your.package.Employer, your.package.Employee</a-hint>
<b-hint>your.DTOpackage.EmployerDTO, your.DTOpackage.EmployeeDTO</b-hint>
</field>
</mapping>
来源:https://stackoverflow.com/questions/8472817/how-to-map-a-field-with-type-as-an-abstract-class-with-dozer