oucem / orika

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

mapNulls doesn't apparently work with collections #149

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

Java 1.7u15
Orika 1.4.4

Receiving a JSON request from the web that I'm converting into a mapped object 
(Venue). This Venue has Set<Event> events. I perform a db lookup to pull back 
the database version of Venue then apply Orika to in-place map from the Venue 
received from the Web into the Venue received from the database.

It's important for my application (using Hibernate) to ensure that the Version 
field (as a Long) is *not* replaced by any Version value from the Web request. 
To achieve this, I have this mapping configuration:

 static {
        final MapperFactory mapperFactory = new DefaultMapperFactory.Builder().mapNulls(false).build();
        mapperFactory.classMap(Venue.class, Venue.class)
                .byDefault()
                .register();
        mapperFactory.getConverterFactory().registerConverter(new PassThroughConverter(DateTime.class)); // DateTime is immutable :-)
        mapperFacade = mapperFactory.getMapperFacade(Venue.class, Venue.class);
    }

The mapping is applied like this:

    public static Venue merge(final Venue fromWeb, final Venue fromDatabase) {
        return mapperFacade.map(fromWeb, fromDatabase);
    }

When I examine the mapped Venue, I see that the Version in the venue has 
successfully been maintained to be the same value as fromDatabase. However, for 
each Event within the Venue, the Version field has been replaced by a null 
(each Event within the Venue from the Database has a value of the Version 
column).

Therefore, it appears at Orika is overwriting each Event's Version field within 
the Set with the value from each Event's Version from the web.

I don't think this should happen. It should not overwrite any of these values 
when I've told it not to map nulls.

-=david=-

Original issue reported on code.google.com by dharri...@gmail.com on 17 Mar 2014 at 10:45

GoogleCodeExporter commented 8 years ago
The default handling of collection in Orika,

dest.collection.clear(); dest.collection.addAll(mapedSourceCollection);

the elements of dest.collection are lost, and Orika create new instance from 
collection source elements.

This is the Collection "merger" by default.

If you want to keep elements on destination collection and just map-in place, 
you have to provide a "merger" to implement your custom logic.

Here is an example of "merger" 
https://github.com/orika-mapper/orika/blob/master/tests/src/main/java/ma/glasnos
t/orika/test/community/CustomMergerTest.java#L157

this use case is used by a lot of people (we can consider to add a builtin 
one). 

Original comment by elaat...@gmail.com on 17 Mar 2014 at 11:06

GoogleCodeExporter commented 8 years ago
Hi,

Thank you for the comment. I will have a look shortly.

I would definitely welcome a built-in function to do in-place collection 
mapping - that would make a lot of people (who have to handle collections!) 
very very happy instead of a clear and replace :-)

-=david=-

Original comment by dharri...@gmail.com on 17 Mar 2014 at 11:55