Saed Hammad
Saed Hammad

Reputation: 96

MapStruct mapping nested beans as is

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

Upvotes: 2

Views: 2021

Answers (3)

Fox2k
Fox2k

Reputation: 21

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}).

Upvotes: 0

Saed Hammad
Saed Hammad

Reputation: 96

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);

    }

Upvotes: 1

Filip
Filip

Reputation: 21393

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

Upvotes: -1

Related Questions