Class JpaMapperUtils


  • public final class JpaMapperUtils
    extends Object
    Utility methods for ModelMapper field mapping special cases.
    • Method Detail

      • populateOrInitializeSet

        public static <S,​D> Set<D> populateOrInitializeSet​(org.modelmapper.ModelMapper mapper,
                                                                 org.modelmapper.spi.MappingContext<Set<S>,​Set<D>> mappingContext,
                                                                 Class<D> destinationElementType)
        This method is intended to be used within a Converter for a property mapping between two Set fields.

        If the destination field is null, then it will initialize its value to a new LinkedHashSet instance before performing the per-element mapping.

        If the destination field is not null, then it will preserve the existing instance and simply clear its contents before performing the per-element mapping. Preserving the existing instance reference is important particularly for scenarios where it is a Hibernate proxy.

        Type Parameters:
        S - the type of the elements in the source field
        D - the type of the elements in the destination field
        Parameters:
        mapper - a ModelMapper instance that already contains a TypeMap from the source element type to the destination element type. It will be used for per-element mapping.
        mappingContext - the mapping context provided to the Converter
        destinationElementType - the type of the elements in the destination field
        Returns:
        the destination field value after performing necessary initializations and mapping
      • populateOrInitializeSet

        public static <S,​D> Set<D> populateOrInitializeSet​(org.modelmapper.spi.MappingContext<Set<S>,​Set<D>> mappingContext,
                                                                 Function<S,​D> destinationFactory)
        This method is intended to be used within a Converter for a property mapping between two Set fields.

        If the destination field is null, then it will initialize its value to a new LinkedHashSet instance before performing the per-element mapping.

        If the destination field is not null, then it will preserve the existing instance and simply clear its contents before performing the per-element mapping. Preserving the existing instance reference is important particularly for scenarios where it is a Hibernate proxy.

        Type Parameters:
        S - the type of the elements in the source field
        D - the type of the elements in the destination field
        Parameters:
        mappingContext - the mapping context provided to the Converter
        destinationFactory - The Function responsible for creating the destination instance
        Returns:
        the destination field value after performing necessary initializations and mapping
      • populateOrInitializeList

        public static <S,​D> List<D> populateOrInitializeList​(org.modelmapper.ModelMapper mapper,
                                                                   org.modelmapper.spi.MappingContext<List<S>,​List<D>> mappingContext,
                                                                   Class<D> destinationElementType)
        This method is intended to be used within a Converter for a property mapping between two List fields.

        If the destination field is null, then it will initialize its value to a new ArrayList instance before performing the per-element mapping.

        If the destination field is not null, then it will preserve the existing instance and simply clear its contents before performing the per-element mapping. Preserving the existing instance reference is important particularly for scenarios where it is a Hibernate proxy.

        Type Parameters:
        S - the type of the elements in the source field
        D - the type of the elements in the destination field
        Parameters:
        mapper - a ModelMapper instance that already contains a TypeMap from the source element type to the destination element type. It will be used for per-element mapping.
        mappingContext - the mapping context provided to the Converter
        destinationElementType - the type of the elements in the destination field
        Returns:
        the destination field value after performing necessary initializations and mapping
      • populateOrInitializeList

        public static <S,​D> List<D> populateOrInitializeList​(org.modelmapper.spi.MappingContext<List<S>,​List<D>> mappingContext,
                                                                   Function<S,​D> destinationFactory)
        This method is intended to be used within a Converter for a property mapping between two List fields.

        If the destination field is null, then it will initialize its value to a new ArrayList instance before performing the per-element mapping.

        If the destination field is not null, then it will preserve the existing instance and simply clear its contents before performing the per-element mapping. Preserving the existing instance reference is important particularly for scenarios where it is a Hibernate proxy.

        Type Parameters:
        S - the type of the elements in the source field
        D - the type of the elements in the destination field
        Parameters:
        mappingContext - the mapping context provided to the Converter
        destinationFactory - The Function responsible for creating the destination instance
        Returns:
        the destination field value after performing necessary initializations and mapping
      • populateOrInitializeListFromSet

        public static <S,​D> List<D> populateOrInitializeListFromSet​(org.modelmapper.spi.MappingContext<Set<S>,​List<D>> mappingContext,
                                                                          Function<S,​D> destinationFactory)
        This method is intended to be used within a Converter for a property mapping between a Set and List fields.

        If the destination field is null, then it will initialize its value to a new ArrayList instance before performing the per-element mapping.

        If the destination field is not null, then it will preserve the existing instance and simply clear its contents before performing the per-element mapping. Preserving the existing instance reference is important particularly for scenarios where it is a Hibernate proxy.

        Type Parameters:
        S - the type of the elements in the source field
        D - the type of the elements in the destination field
        Parameters:
        mappingContext - the mapping context provided to the Converter
        destinationFactory - The Function responsible for creating the destination instance
        Returns:
        the destination field value after performing necessary initializations and mapping