Skip to content

Instantly share code, notes, and snippets.

@dehidehidehi
Created July 28, 2022 09:17
Show Gist options
  • Save dehidehidehi/0ac6fcbb3cb9dfd6cedd3091aedbfb6c to your computer and use it in GitHub Desktop.
Save dehidehidehi/0ac6fcbb3cb9dfd6cedd3091aedbfb6c to your computer and use it in GitHub Desktop.
Contract for a generic dto to entity mapper from JHipster.
import java.util.List;
import org.mapstruct.BeanMapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;
import org.mapstruct.NullValuePropertyMappingStrategy;
/**
* Contract for a generic dto to entity mapper.
*
* @param <D> - DTO type parameter.
* @param <E> - Entity type parameter.
*/
public interface EntityMapper<D, E> {
E toEntity(D dto);
D toDto(E entity);
List<E> toEntity(List<D> dtoList);
List<D> toDto(List<E> entityList);
@Named("partialUpdate")
@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
void partialUpdate(@MappingTarget E entity, D dto);
}
import org.mapstruct.*;
/**
* Mapper for the entity {@link ExampleEntity} and its DTO {@link ExampleEntityDTO}.
*/
@Mapper(componentModel = "spring")
public interface ExampleEntityMapper extends EntityMapper<ExampleEntityDTO, ExampleEntity> {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment