Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no setter methods in generated mapstruct implementation

I'm experimenting with mapstruct and follow this tutorial:

mapstruct tut

I have this entity:

@Entity
public class Company {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_company")
    @SequenceGenerator(name = "seq_company", allocationSize = 1)
    @Column(nullable = false, updatable = false)
    private Long id;
    private String name;
    private String shortName;

    public Company() {
    }

    public Company(Long id, String name, String shortName) {
        this.id = id;
        this.name = name;
        this.shortName = shortName;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }
}

And this is the simple dto:

public class CompanyDto {
    @JsonProperty("id")
    private Long id;
    @JsonProperty("name")
    private String name;
    @JsonProperty("shortName")
    private String shortName;
}

And here is the mapper interface:

@Mapper(componentModel = "spring")
public interface CompanyMapper {
    CompanyDto companyToCompanyDto(Company company);
    Company companyDtoToCompany(CompanyDto companyDto);
    List<CompanyDto> companiesToCompanyDtos(List<Company> companies);
}

I certanly oversee something, because there is no setters in the generated implementation, f. e.:

@Override
    public Company companyDtoToCompany(CompanyDto companyDto) {
        if ( companyDto == null ) {
            return null;
        }

        Company company = new Company();

        return company;
    }

What goes here wrong?

like image 622
derstauner Avatar asked Feb 02 '26 14:02

derstauner


1 Answers

I've noticed that your CompanyDto class has private fields but no getters or setters. There is no standard way to access the fields in that class. You might need to add those in order to map in or out of that class.

like image 131
Zack Peterson Avatar answered Feb 04 '26 05:02

Zack Peterson



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!