简体   繁体   English

生成的 mapstruct 实现中没有 setter 方法

[英]no setter methods in generated mapstruct implementation

I'm experimenting with mapstruct and follow this tutorial:我正在尝试使用 mapstruct 并遵循本教程:

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:这是简单的 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, fe:我确实监督了一些事情,因为生成的实现中没有设置器,fe:

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

        Company company = new Company();

        return company;
    }

What goes here wrong?这里出了什么问题?

I've noticed that your CompanyDto class has private fields but no getters or setters.我注意到您的 CompanyDto class 具有私有字段,但没有获取器或设置器。 There is no standard way to access the fields in that class.没有标准方法可以访问 class 中的字段。 You might need to add those in order to map in or out of that class.您可能需要将这些添加到 map 进出 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM