3

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. That was the issue. In the linked tutorial, the author has used lombok (getter and setter) annotations. Now, it works.
Suppose there intentionally is no setter method in order to preserve encapsulation of a field. Think of a collection field that has an add method but no set method. What could be the (non-) standard way to deal with that?
If using Lombok, annotate the Dto class with @Builder which is a creation design pattern designed to preserve encapsulation (no setters). Mapstruct will recognise the builder pattern and generate mappers that use the builder instead of setters - you will see this in the generated code. Preserving encapsulation is always good practice for DTOs in my opinion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.