user1007522
user1007522

Reputation: 8118

ModelMapper is taking toString method to convert property?

I'm using ModelMapper to map Entities to DTO's in my spring boot application.

Now I have something strange I have this entity:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MileStoneEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    @ManyToOne(targetEntity = ProjectEntity.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "projectEntity_id")
    private ProjectEntity project;
    @ManyToMany(targetEntity = CompanyUserEntity.class)
    private Set<CompanyUserEntity> projectManagers;
    @OneToMany(targetEntity = TaskEntity.class, mappedBy = "mileStone")
    private Set<TaskEntity> tasks;
    private boolean archived;

        public List<Long> projectManagersIds() {
        return projectManagers.stream().map(CompanyUserEntity::getId).collect(Collectors.toList());
    }

    public List<Long> taskIds() {
        return tasks.stream().map(TaskEntity::getId).collect(Collectors.toList());
    }
}

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TaskEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    @ManyToOne(targetEntity = ProjectEntity.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "projectEntity_id")
    @ManyToOne(targetEntity = CompanyUserEntity.class, fetch = FetchType.LAZY)
    private CompanyUserEntity executor;
    @ManyToOne(targetEntity = MileStoneEntity.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "mileStoneEntity_id")
    private MileStoneEntity mileStone;
    private boolean archived;
    private boolean replaceStartAndEndDateWithMileStone;
}

public static PropertyMap<MileStoneEntity, MileStoneDto> mileStoneEntityToMap = new PropertyMap<MileStoneEntity, MileStoneDto>() {
    protected void configure() {
        map().setProjectId(source.getProject().getId());
        map().setProjectManagers(source.projectManagersIds());
        map().setTasks(source.taskIds());
    }
};

DTO

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MileStoneDto {
    private Long id;
    private String name;
    private Long projectId;
    private List<Long> projectManagers;
    private LocalDate startDate;
    private LocalDate endDate;
    private String description;
    private List<Long> tasks;
    private boolean archived;
}

Now this doesn't work I get this error:

"ModelMapper mapping errors:\n\n1) Converter org.modelmapper.internal.converter.NumberConverter@ad01ae2 failed to convert entity.TaskEntity to java.lang.Long

But when I then add a toString method like this in the TaskEntity:

@Override
public String toString() {
    return ""+id+"";
}

then everything works. So why is ModelMapper taking the toString to convert the id to a Long?

EDIT

@Bean
ModelMapper modelMapper() {
    ModelMapper modelMapper = new ModelMapper();

    modelMapper.addMappings(ProjectMappings.mileStoneEntityToMap);


    return modelMapper;
}

Upvotes: 0

Views: 1469

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36203

You don't have the same types in your entity and your dto:

Entity

private Set<TaskEntity> tasks;

DTO

private List<Long> tasks;

ModelMapper does not know how to convert the TaskEntity to a Long.

You have to write a converter for this purpose:

http://modelmapper.org/user-manual/converters/

Upvotes: 2

Related Questions