1

My question is how to map object contains nested object to DTO as is, not a value from the nested object, for example if there is 2 classes like this :

    public class TestClass {
        @Id
        private String id;

        @Field("field1")
        private String field1;

        @Field("field2")
        private Long field2;

        @Field("nestedClass")
        private NestedClass;

        //getter & setter 
    }

    public class NestedClass {
    //fields and getter and setter for it
    }

    DTO classes looks like :

    public class TestClassDTO {
        private String id;
        private String field1;
        private Long field2;
        private NestedClassDTO ;

        //getter & setter 
    }

    public class NestedClassDTO {
        //fields and getter and setter for it
    }


    @Mapper(componentModel = "spring", uses = {})
    public interface TestClassMapper {

        TestClassDTO TestClassToTestClassDTO(TestClass TestClass);

        TestClass TestClassDTOToTestClass(TestClassDTO TestClassDTO);

        NestedClass NestedClassDTOToNestedClass(NestedClassDTO NestedClassDTO);

        NestedClassDTO NestedClassToNestedClassDTO(NestedClass NestedClass);

        }

after invoking TestClassDTOToTestClass() and sending TestClassDTO contains NestedClassDTO .. it is return TestClass with null NestedClass .. is it possible to map it without write my own mapper ?

SH

lrkwz
  • 6,105
  • 3
  • 36
  • 59
Saed Hammad
  • 96
  • 10

3 Answers3

2

I found it :)

the mapper class should use the nestedclass like this :

    @Mapper(componentModel = "spring", uses = {NestedClassMapper.class})
    public interface TestClassMapper {

        TestClassDTO TestClassToTestClassDTO(TestClass TestClass);

        TestClass TestClassDTOToTestClass(TestClassDTO TestClassDTO);

        NestedClass NestedClassDTOToNestedClass(NestedClassDTO NestedClassDTO);

        NestedClassDTO NestedClassToNestedClassDTO(NestedClass NestedClass);

    }
Saed Hammad
  • 96
  • 10
  • If your `TestClassMapper` really looks like this, then you should not need `NestedClassMapper`. The reason for this is that the mapping between `NestedClass` and `NestedClassDTO` and the opposite is already defined in the `TestClassMapper`. – Filip Apr 16 '17 at 23:08
  • I'm not sure why.. but it didn't work without add NestedClassMapper.class in uses @Mapper – Saed Hammad Apr 17 '17 at 10:55
  • 1
    If that was the case it might be a bug. Can you perhaps share the `NestedClassMapper` as well, so I can have a better look at it? – Filip Apr 17 '17 at 14:53
0

Names of the field in the TestClass and TestClassDTO must be the same, then you won't need all this (componentModel = "spring", uses = {NestedClassMapper.class}).

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
Fox2k
  • 21
  • 2
-1

If I understood you correctly you want to have a mapping method that will not map (ignore) the nestedClassfield from the TestClassDTO. You can use @Mapping#ignore.

Your mapper will look something like:

@Mapper(componentModel = "spring", uses = {})
public interface TestClassMapper {

    TestClassDTO TestClassToTestClassDTO(TestClass TestClass);

    @Mapping(target = "nestedClass", ignore = true)
    TestClass TestClassDTOToTestClass(TestClassDTO TestClassDTO);

    NestedClass NestedClassDTOToNestedClass(NestedClassDTO NestedClassDTO);

    NestedClassDTO NestedClassToNestedClassDTO(NestedClass NestedClass);

    }

If for some reason you want explicitly set the value to null then have a look a the answer from this question

Community
  • 1
  • 1
Filip
  • 19,269
  • 7
  • 51
  • 60