Backend

[SpringBoot] - MapStruct entity <-> Id 간 매핑

GOMJ 2023. 12. 12. 13:37

Mapstruct를 쓰며 entity -> long, long -> entity 매핑이 필요했다.

@Mapper
public interface AMapper {

    @Mapping(source = "bs", target = "bIds", qualifiedByName = "entityToId")
    public ADto aToADto(A a);

    @Named("entityToId") 
    public static Long entityToId(Entity entity) { 
        return entity.getId(); 
    }

    @Named("idToEntity")
    public static Entity idToEntity(Long id){
        return Entity.builder().id(id).build();
    }
}

커스텀으로 매핑 관계를 지정해주고 qualifiedByName에 적용하면 끝이다.