I’m starting my very first steps with Mapstruct mapper. I want to map a JPA data entity class to a DTO class. This is my source class:
@Entity
@Data
@Table(name = "projects")
public class Project {
@Id
private Long Id;
private String projectName;
private String description;
@OneToMany(mappedBy = "project")
List<Sprint> sprints;
@OneToMany(mappedBy = "project")
List<Epic> epics;
@OneToMany(mappedBy = "project")
List<Story> stories;
public Project(Long id, String projectName, String description) {
Id = id;
this.projectName = projectName;
this.description = description;
}
}
This is my target class:
@Data
@AllArgsConstructor
public class ProjectDTO {
private Long Id;
private String projectName;
private String description;
}
The @Data annotation is from Lombok. I want to make a mapper to map the Project to ProjectDTO, the attributes like sprints, epics, stories SHOULD NOT be included in ProjectDTO. This is my mapper interface:
@Mapper
public interface ProjectMapper extends Mapper {
ProjectMapper INSTANCE = Mappers.getMapper(ProjectMapper.class)
ProjectDTO projectToProjectDTO(Project project);
}
When I try to build it, this is the error message I got:
[ERROR] Can't generate mapping method with no input arguments.
I guess it’s related to the missing properties in ProjectDTO, but don’t know to solve it. With the @Mapping, I cannot do it like:
@Mapping(source=“sprints”, target= null)
Any help would be appreciated!