简体   繁体   English

如何使用dozer将类型的字段映射为抽象类?

[英]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. 我的问题涉及将字段Contract.contractor从B映射到A,因为字段Contract.contractor是一个抽象类,推土机无法猜测如何实例化它。

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 ? 所以我的问题很简单:我如何向dozer表示,对于字段Contract.contractor的映射,它应该实例化一个Employer实例,如果type == 'Employer '和其他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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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