简体   繁体   English

Orika映射嵌套子列表

[英]Orika mapping nested sublist

Consider the below example. 考虑下面的示例。

Group.java(GroupDTO.java also has the same properties) Group.java(GroupDTO.java也具有相同的属性)

import java.util.List;

public class Group {
    public Group(List<Person> persons) {
        this.persons = persons;
    }

    private List<Person> persons;

    public List<Person> getPersons() {
        return persons;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }
}

Person.java(PersonDTO.java also has the same properties) Person.java(PersonDTO.java也具有相同的属性)

import java.util.List;

public class Person {

    public Person(){}
    public Person(List<Name> names, Integer age) {
        this.names = names;
        this.age = age;
    }

    private List<Name> names;
    private Integer age;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public List<Name> getNames() {
        return names;
    }

    public void setNames(List<Name> names) {
        this.names = names;
    }
}

Name.java(NameDTO.java also has the same properties) Name.java(NameDTO.java也具有相同的属性)

public class Name {
    public Name(String first, String last) {
        this.first = first;
        this.last = last;
    }

    private String first;
    private String last;

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getLast() {
        return last;
    }

    public void setLast(String last) {
        this.last = last;
    }
}

And when I run the below class I get the following exception. 当我运行下面的类时,出现以下异常。

import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.ClassMapBuilder;

import java.util.LinkedList;
import java.util.List;

public class Mapper {

    public static void main(String[] args) {

        List<Name> names = new LinkedList<Name>();
        names.add(new Name("foo", "bar"));
        names.add(new Name("lorem", "ipsum"));
        List<Person> persons = new LinkedList<Person>();
        persons.add(new Person(names, 30));
        persons.add(new Person(names, 40));
        Group g1 = new Group(persons);

        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        ClassMapBuilder classMapBuilder = mapperFactory.classMap(Group.class, GroupDTO.class);
        classMapBuilder.field("persons{age}", "persons{age}");
        classMapBuilder.field("persons{names{first}}", "persons{names{first}}");
        classMapBuilder.field("persons{names{last}}", "persons{names{last}}");
        classMapBuilder.byDefault().register();

        MapperFacade mapper = mapperFactory.getMapperFacade();
        GroupDTO groupDTO = mapper.map(g1, GroupDTO.class);

        for(Person p:groupDTO.getPersons()){
            System.out.println(p.getAge());
            for(Name n:p.getNames()){
                System.out.println(n.getFirst());
                System.out.println(n.getFirst());
            }
        }

    }
}

Exception 例外

Exception in thread "main" ma.glasnost.orika.MappingException: java.lang.NullPointerException
    at ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:104)
    at ma.glasnost.orika.impl.DefaultMapperFactory.buildMapper(DefaultMapperFactory.java:1480)
    at ma.glasnost.orika.impl.DefaultMapperFactory.build(DefaultMapperFactory.java:1295)
    at ma.glasnost.orika.impl.DefaultMapperFactory.getMapperFacade(DefaultMapperFactory.java:883)
    at Mapper.main(Mapper.java:28)
Caused by: java.lang.NullPointerException
    at ma.glasnost.orika.impl.generator.specification.MultiOccurrenceToMultiOccurrence.registerClassMaps(MultiOccurrenceToMultiOccurrence.java:397)
    at ma.glasnost.orika.impl.generator.specification.MultiOccurrenceToMultiOccurrence.generateMappingCode(MultiOccurrenceToMultiOccurrence.java:72)
    at ma.glasnost.orika.impl.generator.SourceCodeContext.mapAggregateFields(SourceCodeContext.java:721)
    at ma.glasnost.orika.impl.generator.MapperGenerator.addMapMethod(MapperGenerator.java:182)
    at ma.glasnost.orika.impl.generator.MapperGenerator.build(MapperGenerator.java:70)
    ... 4 more

Sorry about the long post. 抱歉,帖子很长。 Am trying map fields from inside List<> which is again inside another List<>. 我正在尝试从List <>内部的地图字段,而List <>也在另一个List <>内部。 What would be the best approach or am I doing something wrong here? 最好的方法是什么,或者我在这里做错了什么?

If the field names on both sides are equal, you don't have to register the field mappings, Orika will handle that by itself. 如果两边的字段名称相等,则不必注册字段映射,Orika会自行处理。 This code will do the mapping correctly: 此代码将正确执行映射:

    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
    ClassMapBuilder classMapBuilder = mapperFactory.classMap(Group.class, GroupDTO.class);
    classMapBuilder.byDefault().register();

    MapperFacade mapper = mapperFactory.getMapperFacade();
    GroupDTO groupDTO = mapper.map(g1, GroupDTO.class);

Also, probably just a typo, but the getPersons() from GroupDTO should return PersonDTO , same for getNames() and NameDTO : 另外,可能只是一个错字,但GroupDTOgetPersons()应该返回PersonDTO ,与getNames()NameDTO

   for(PersonDTO p:groupDTO.getPersons()){
        System.out.println(p.getAge());
        for(NameDTO n:p.getNames()){
            System.out.println(n.getFirst());
            System.out.println(n.getLast());
        }
    }

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

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