How to map a field with type as an abstract class with dozer?

随声附和 提交于 2019-12-23 13:15:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!