Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't generate mapping method with no input arguments with Mapstruct

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!

like image 844
Andiana Avatar asked Jun 25 '26 10:06

Andiana


1 Answers

Add the '@NoArgConstructor' as well. MapStruct cannot (yet) deal with constructing objects via constructor. Another option would be using '@Builder' in stead if your objects are truly immutable

like image 102
Sjaak Avatar answered Jun 26 '26 22:06

Sjaak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!