Last active
April 1, 2021 19:24
-
-
Save evrentan/d2b8496264276c069efc287fc99c1e8b to your computer and use it in GitHub Desktop.
EnterpriseMapper Class Implemented with MapStruct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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