Mapstruct: Clear Collection on update when using Adders

前端 未结 1 329
情书的邮戳
情书的邮戳 2021-01-22 19:00

I try to map my DTO objects to my JPA entities. I have a Collection of children in my ParentEntity. They can be added addChild(). Using th

相关标签:
1条回答
  • 2021-01-22 19:30

    The text in the documentation need to be rephrased. The problem is that especially for collections, there's no good way to handle this out of the box in MapStruct. I'm currently writing some new text for the documentation.

    Consider this (when thinking what MapStruct should do for updating collections in general):

    • What if there's no match: should the non-matching elements be removed?
    • Should the non matching source elements be added?
    • What exactly constitutes to a match: equals? hashcode? comparator==0?
    • Can there be more than one match (Lists, but also depending on what is considered a match.)
    • How should the resulting collection be sorted?
    • Should a newly created object be added to a persistence context?
    • What about JPA child-parent relations?

    About the latter one, Dali (Eclipse) also generates remove methods. So should MapStruct call these in the light of the above?

    At this moment it works like this: whenever the user wants a collection update method, MapStruct generates a regular call to element mappings (in stead of an update call), because it is the only sensible thing to do. All the remainder is highly dependent on the use-case. If you need to clear the collection at before hand, use the @BeforeMapping to clear it.

    Note: I just fixed an issue that handles also adders in this fashion in stead of the vague error message you get now.

    If you want a nice way to handle child/parent relations and integrate them with JPA.. have a look at the examples.

    0 讨论(0)
提交回复
热议问题