Skip to content

Instantly share code, notes, and snippets.

@evrentan
Last active April 1, 2021 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evrentan/d2b8496264276c069efc287fc99c1e8b to your computer and use it in GitHub Desktop.
Save evrentan/d2b8496264276c069efc287fc99c1e8b to your computer and use it in GitHub Desktop.
EnterpriseMapper Class Implemented with MapStruct
package com.tan.example.mapper;
import com.tan.example.dto.Enterprise;
import com.tan.example.entity.EnterpriseEntity;
import org.bson.types.ObjectId;
import org.mapstruct.*;
import java.util.List;
import java.util.Objects;
@Mapper(componentModel = "cdi",
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface EnterpriseMapper {
@Mapping(target = "id", ignore = true)
EnterpriseEntity toEntity(Enterprise enterprise);
@Mapping(target = "id", expression = "java(enterpriseEntity.id.toString())")
Enterprise toDto(EnterpriseEntity enterpriseEntity);
List<EnterpriseEntity> toEntityList(List<Enterprise> enterpriseList);
List<Enterprise> toDtoList(List<EnterpriseEntity> enterpriseEntityList);
@AfterMapping
default void setEntityId(Enterprise enterprise, @MappingTarget EnterpriseEntity enterpriseEntity) {
if(Objects.nonNull(enterprise.id))
enterpriseEntity.id = new ObjectId(enterprise.id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment