推土机 – 排除嵌套对象

huangapple go评论79阅读模式
英文:

Dozer - Excluding nested Objects

问题

根据这个示例,您可以使用Dozer进行深度映射:

http://dozer.sourceforge.net/documentation/deepmapping.html

> <mapping>
> <class-a>com.github.dozermapper.core.vo.deep.SrcDeepObj</class-a>
> <class-b>com.github.dozermapper.core.vo.deep.DestDeepObj</class-b>
> <field>
> <a>srcNestedObj.src1</a>
> <b>dest1</b>
> </field>
> <field>
> <a>srcNestedObj.src2</a>
> <b>dest2</b>
> </field>
> <field>
> <a>srcNestedObj.srcNestedObj2.src5</a>
> <b>dest5</b>
> </field>
> </mapping>

类似地,我想要排除一些嵌套的对象和字段,因此我尝试了同样的方式:

> <mapping map-id="case1" map-null="false">
> <class-a>com.server.dal.entities.ClassA</class-a>
> <class-b>com.server.dal.dto.ClassB</class-b>
>
> <field-exclude>
> <a>childObj1.childObj2</a>
> <b>childObj1.childObj2</b>
> </field-exclude>
> <field-exclude>
> <a>childObj1.childObj3.testMap</a>
> <b>childObj1.childObj3.testMap</b>
> </field-exclude>
> </mapping>

但是这并没有按预期工作。是否有其他方法可以排除嵌套对象的字段?

英文:

According to this example you can do Deep Mapping with Dozer :

http://dozer.sourceforge.net/documentation/deepmapping.html

> <mapping>
> <class-a>com.github.dozermapper.core.vo.deep.SrcDeepObj</class-a>
> <class-b>com.github.dozermapper.core.vo.deep.DestDeepObj</class-b>
> <field>
> <a>srcNestedObj.src1</a>
> <b>dest1</b>
> </field>
> <field>
> <a>srcNestedObj.src2</a>
> <b>dest2</b>
> </field>
> <field>
> <a>srcNestedObj.srcNestedObj2.src5</a>
> <b>dest5</b>
> </field>
> </mapping>

Similarly I want to exclude few nested objects and fields, and so I tried same way :

> <mapping map-id="case1" map-null="false">
> <class-a>com.server.dal.entities.ClassA</class-a>
> <class-b>com.server.dal.dto.ClassB</class-b>
>
> <field-exclude>
> <a>childObj1.childObj2</a>
> <b>childObj1.childObj2</b>
> </field-exclude>
> <field-exclude>
> <a>childObj1.childObj3.testMap</a>
> <b>childObj1.childObj3.testMap</b>
> </field-exclude>
> </mapping>

But this is not working as expected. Is there any other way how to exclude nested Object's fields?

答案1

得分: 7

你可以按照以下方式排除嵌套对象:

<mapping map-id="caseClassA" map-null="false">
    <class-a>com.server.dal.entities.ClassA</class-a>
    <class-b>com.server.dal.dto.ClassB</class-b>
    <field map-id="excludeSubItem1">
        <a>childObj1</a>
        <b>childObj1</b>
    </field>
</mapping>

<mapping map-id="excludeSubitem1" map-null="false">
    <class-a>com.server.dal.entities.ClassChild1</class-a>
    <class-b>com.server.dal.dto.ClassChild1</class-b>
    <field-exclude>
        <a>childObj2</a>
        <b>childObj2</b>
    </field-exclude>
    <field map-id="excludeSubItem2">
        <a>childObj3</a>
        <b>childObj3</b>
    </field>
</mapping>

<mapping map-id="excludeSubitem2" map-null="false">
    <class-a>com.server.dal.entities.ClassChild3</class-a>
    <class-b>com.server.dal.dto.ClassChild3</class-b>
    <field-exclude>
        <a>testMap</a>
        <b>testMap</b>
    </field-exclude>
</mapping>
英文:

You can exclude nested objects as follows :

> <mapping map-id="caseClassA" map-null="false">
> <class-a>com.server.dal.entities.ClassA</class-a>
> <class-b>com.server.dal.dto.ClassB</class-b>
> <field map-id="excludeSubItem1">
> <a>childObj1</a>
> <b>childObj1</b>
> </field>
> </mapping>

> <mapping map-id="excludeSubitem1" map-null="false">
> <class-a>com.server.dal.entities.ClassChild1</class-a>
> <class-b>com.server.dal.dto.ClassChild1</class-b>
> <field-exclude>
> <a>childObj2</a>
> <b>childObj2</b>
> </field-exclude>
> <field map-id="excludeSubItem2">
> <a>childObj3</a>
> <b>childObj3</b>
> </field>
> </mapping>

> <mapping map-id="excludeSubitem2" map-null="false">
> <class-a>com.server.dal.entities.ClassChild3</class-a>
> <class-b>com.server.dal.dto.ClassChild3</class-b>
> <field-exclude>
> <a>testMap</a>
> <b>testMap</b>
> </field-exclude>
> </mapping>

huangapple
  • 本文由 发表于 2020年10月11日 19:47:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64303652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定