简体   繁体   English

Orika映射嵌套的多次出现元素

[英]Orika Mapping nested multi-occurrence elements

I want map all persons (mens and womens) to the same PersonDto with Orika. 我想将所有人(男人和女人)映射到与Orika相同的PersonDto。

class Name {
   private String first;
   private String last;
   private String fullName;
   // getters/setters 
}

class Womens{
   private List<Name> names;
   // getters/setters 
}

class Mens{
   private List<Name> names;
   // getters/setters 
}

class Person {
   private Mens mens;
   private Womens womens;
   // getters/setters 
}

class PersonDto { 
  private List<Info> info;
  // getters/setters omitted
}

class Info { 
  private String notes;
  // getters/setters omitted
}

If I test with mens only, is it OK. 如果我只测试男士,可以吗?

mapperFactory.classMap(Person.class, PersonDto.class)
       .field("mens.names{first}", "info[0].notes")
       .field("mens.names{last}", "info[1].notes")
       .field("mens.names{fullName}", "info[2].notes")
       .register();

If I test with womens only, is it OK, 如果我只和女子一起测试,可以吗?

mapperFactory.classMap(Person.class, PersonDto.class)
       .field("womens.names{first}", "info[0].notes")
       .field("womens.names{last}", "info[1].notes")
       .field("womens.names{fullName}", "info[2].notes")
       .register();

but if I test with mens and womens, is it KO. 但是,如果我测试男女装,那是KO。 info array do not have the good size 信息数组的大小不正确

mapperFactory.classMap(Person.class, PersonDto.class)
       .field("mens.names{first}", "info[0].notes")
       .field("mens.names{last}", "info[1].notes")
       .field("mens.names{fullName}", "info[2].notes")
       .field("womens.names{first}", "info[3].notes")
       .field("womens.names{last}", "info[4].notes")
       .field("womens.names{fullName}", "info[5].notes")
       .register();

I find a solution but is it not the best. 我找到了解决方案,但这不是最好的。 I you have a better respons, please post here you solution. 如果您有更好的回应,请在此处发布您的解决方案。

I split to two Infomaton class: 我分为两个Infomaton类:

@XmlRootElement(name = "notes")
class InfoMen { 
    private String notes;
    // getters/setters omitted
}

@XmlRootElement(name = "notes")
class InfoWomen { 
    private String notes;
    // getters/setters omitted
}

@XmlRootElement(name = "persons")
class PersonDto { 
    private List<InfoMen> infoMen;
    private List<InfoWomen> infoWomen;

    @XmlElement(name = "notes")
    public List<InfoMen> getInfoMen() {
        return infoMen;
    }

    @XmlElement(name = "notes")
    public List<InfoWomen> getInfoWomen() {
        return infoWomen;
    }
    // setters omitted
}

My XML output is OK but my PersonDto object contains two objects instand of one. 我的XML输出正常,但是我的PersonDto对象包含两个对象。

<persons>
    <notes>
        <first>Petter</first>
        <last>Butter</last>
        <fullName>Petter Butter<fullName>
    </notes>
    <notes>
        <first>Wenddy</first>
        <last>Window</last>
        <fullName>Wenddy Window<fullName>
    </notes>
</persons>

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

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