After converting to dto child set order jumbles up

Viewed 122

After converting to dto using mapstruct, child id order jumbles up. If i return just the entity without converting to dto the child is properly ordered

"data":[ { "personName":"TestUser", "cars":[ { "id":"2021-000001" }, { "id":"2021-000003" }, { "id":"2021-000002" }, { "id":"2021-000004" } } ]

1 Answers

The reason why the order is different is due to the fact that MapStruct used a HashSet by default when mapping into a Set.

The current implementation types for MapStruct are the following:

Interface type Implementation type
Iterable ArrayList
Collection ArrayList
List ArrayList
Set HashSet
SortedSet TreeSet
NavigableSet TreeSet
Map HashMap
SortedMap TreeMap
NavigableMap TreeMap
ConcurrentMap ConcurrentHashMap
ConcurrentNavigableMap ConcurrentSkipListMap

This can be seen in Implementation types used for collection mappings

There is currently a feature request to change this into an order preserving set / map and it will be included in 1.5.

At the moment you can fix this by using an @ObjectFactory that will return a LinkedHashSet

Related