如何使用推土机将类型字段映射为抽象类?

发布于 2024-12-20 15:55:00 字数 1088 浏览 1 评论 0原文

我有以下域结构:

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; }

现在,当我设置以下推土机映射时:

<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>

我的问题涉及字段 Contract.contractor 从 B 到 A 的映射,因为字段 Contract.contractor 是一个抽象类,推土机无法猜测如何实例化它。

所以我的问题很简单:我如何向推土机指示,对于字段 Contract.contractor 的映射,如果 type == 'Employer ,它应该实例化 Employer 的实例>' 以及其他Employee

感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风启觞 2024-12-27 15:55:00

您可以通过提示来完成此操作。
有点像这样:

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文