404-not-find / orika

Automatically exported from code.google.com/p/orika
2 stars 0 forks source link

Orika creates a new instance of nested list instead of using existing instance #181

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
--------------------------------------
1. Source classes with nested list (CustomerDTO and CustomerAddressDTO. 
CustomerDTO has List<CustomerAddressDTO>) Destination classes also with nested 
List (Customer and CustomerAddress)
2. Create source classes and add data
3. Create destination classes and add data
4. Map from source to destination
5. Notice Orika creates new Customer.List<CustomerAddress> instead of merging 
into existing list.

What is the expected output? What do you see instead?
-----------------------------------------------------
Orika should merge the data from source list into destination list

What version of the product are you using? On what operating system?
--------------------------------------------------------------------
1.4.5

Please provide any additional information below.
------------------------------------------------
Mapping code:
    public void doMap() {
        // Create Mapper
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        MapperFacade mapper = mapperFactory.getMapperFacade();

        // Source
        CustomerDTO cDTO = new CustomerDTO();
        cDTO.setId(10);
        cDTO.setName("ABCD");
        List<CustomerAddressDTO> caDTOList = new ArrayList<CustomerAddressDTO>();
        cDTO.setCustomeraddresses(caDTOList);
        CustomerAddressDTO caDTO1 = new CustomerAddressDTO();
        caDTO1.setId(11);
        caDTO1.setLine("BLR");
        caDTOList.add(caDTO1);

        // Destination
        Customer c = new Customer();
        c.setId(10);
        c.setName("ABCD");
        List<CustomerAddress> caList = new ArrayList<CustomerAddress>();
        c.setCustomeraddresses(caList);
        CustomerAddress ca1 = new CustomerAddress();
        ca1.setId(11);
        ca1.setLine("BLR");
        ca1.setCreatedDateTime(new Date());
        caList.add(ca1);

        // Map
        System.out.println("Before mapping:" + c);

        mapper.map(cDTO, c);

        System.out.println("After mapping:" + c);       
    }

Before mapping:Customer [id=10, name=ABCD, customeraddresses=[CustomerAddress 
[id=11, line=BLR, createdDateTime=Sat Nov 15 10:11:16 IST 2014]]]
After mapping:Customer [id=10, name=ABCD, customeraddresses=[CustomerAddress 
[id=11, line=BLR, createdDateTime=null]]]
Note: createdDateTime has been set to null indicating a new CustomerAddress is 
creating instead of mapping to the existing one

(A sample standalone project attached)

Original issue reported on code.google.com by gam...@gmail.com on 15 Nov 2014 at 4:49

Attachments:

GoogleCodeExporter commented 8 years ago
This is the behaviour by default, but you can use what ever strategy you want 
for merging all you have to do is to is to provide a mapper for List to List 
and add your custom logic there.

Take a look at this example : 
https://github.com/orika-mapper/orika/blob/master/tests/src/main/java/ma/glasnos
t/orika/test/community/CustomMergerTest.java#L157

Original comment by elaat...@gmail.com on 15 Nov 2014 at 11:50

GoogleCodeExporter commented 8 years ago
Thanks for the response. Looking at the example, it seems we will need to 
provide a custom mapper for every entity-dto pair. This seems too effort 
intensive. It will good to have a generic in-built merging algorithm.

Original comment by gam...@gmail.com on 15 Nov 2014 at 4:17