dimanche 1 juillet 2018

Set item from sources class like zero item of list in dest class with Dozer

I have sources classes:

  @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class User {
        private String name;
        private int age;
        private Test test;
    }

  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  public class Test {
      private String hz1;
      private String hz2;
  }

And I have dest classes:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserDest {
    private String name;
    private int age;
    private List<TestDest> tests;
}

Data
@NoArgsConstructor
@AllArgsConstructor
public class TestDest {
    private String hz1;
    private String hz2;
}

I create User and try map it to UserDest:

    DozerBeanMapper mapper = new DozerBeanMapper();
    User user = new User("Bill", 27, new Test("p1", "p2"));
    UserDest userDest = mapper.map(user, UserDest.class);
    System.out.println(userDest);

Output: UserDest(name=Bill, age=27, tests=null)

I add Mapping:

mapper.addMapping(new BeanMappingBuilder() {
            @Override
            protected void configure() {
                TypeMappingBuilder mapping = mapping(User.class, UserDest.class);
                mapping.fields(field("test.hz1"), field("tests[0].hz1"));
                mapping.fields(field("test.hz2"), field("tests[0].hz2"));
            }
        });

And after that output:

UserDest(name=Bill, age=27, tests=[TestDest(hz1=p1, hz2=p2)])

But I need set each field like this

User test.hz1 set to UserDest tests[0].hz1

I want next:

mapping.fields(field("test"), field("tests[0]"));

But this after that I get this output:

UserDest(name=Bill, age=27, tests=[[]])

How can I set one item from sources class to zero item of list in dest class? Maby some converter or somthing else?





Aucun commentaire:

Enregistrer un commentaire