简体   繁体   中英

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

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM