Can mapstruct @SubclassMapping annotation be combined with an @Mapping for the subclass?

Using mapstruct v. 1.5.5, java v. 22.

Let’s say I have some entities that I am wanting to map to DTOs. The DTOs are java records.

My base entity looks like this (simplified):

public abstract class DateRule implements Predicate<LocalDate>
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

}

Let’s say I then have two subclasses from DateRule: AnnuaDateRule and DayOfWeekRule. I then have two java records for AnnualDateRuleDTO and DayOfWeekRuleDTO. The records are defined as follows:

public record AnnualDateRuleDTO(Long id, String name, @NotNull Month targetMonth, @NotNull DayOfWeek targetDayOfWeek, @NotNull int nthOccurrence) implements DateRuleDTO {}


public record DayOfWeekRuleDTO(Long id, String name, @NotNull DayOfWeek dayToMatch) implements DateRuleDTO {}

My mapper class is defined as follows:

@Mapper(componentModel = "spring", subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION)
public interface DateRuleDTOMapper
{
    @SubclassMapping(source = AnnualDateRule.class, target = AnnualDateRuleDTO.class)
    @SubclassMapping(source = DayOfWeekRule.class, target = DayOfWeekRuleDTO.class)
    DateRuleDTO dateRuleToDTO(DateRule rule);

    @SubclassMapping(source = AnnualDateRuleDTO.class, target = AnnualDateRule.class)
    @SubclassMapping(source = DayOfWeekRuleDTO.class, target = DayOfWeekRule.class)
    DateRule dateRuleFromDTO(DateRuleDTO dto);
}

According to the javadocs for the @SubclassMapping annotation, “This annotation can be combined with @Mapping annotations.” However, I don’t see any documentation on how to do so.

At compile time, I get warnings that the id field is unmapped DateRuleDTOMapper.java:18: warning: Unmapped target property: "id". Mapping from SubclassMapping for us.lawyertools.datecalculator.core.AnnualDateRule "AnnualDateRule annualDateRule" to "AnnualDateRuleDTO annualDateRuleDTO". DateRuleDTO dateRuleToDTO(DateRule rule); I get similar warnings for the other classes.

So, even though the AnnualDateRule inherits the ‘id’ field from its parent, DateRule, mapstruct is not mapping it to the target ‘id’ property on AnnualDateRuleDTO.

At runtime, as you would expect given the warning, the mapper returns the DTO records with id being null.

My attempt to annotate the above dateRuleToDTO method with an @Mapping results in a compiler error. With the @Mapping annotation, the mapper looks as follows:

@Mapper(componentModel = "spring", subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION)
public interface DateRuleDTOMapper
{
    @SubclassMapping(source = AnnualDateRule.class, target = AnnualDateRuleDTO.class)
    @SubclassMapping(source = DayOfWeekRule.class, target = DayOfWeekRuleDTO.class)
    @Mapping(source="id", target="id")
    DateRuleDTO dateRuleToDTO(DateRule rule);

    @SubclassMapping(source = AnnualDateRuleDTO.class, target = AnnualDateRule.class)
    @SubclassMapping(source = DayOfWeekRuleDTO.class, target = DayOfWeekRule.class)
    DateRule dateRuleFromDTO(DateRuleDTO dto);
}

The compiler error: DateRuleDTOMapper.java:18: error: No property named "id" exists in source parameter(s). Did you mean "name"? @Mapping(source="id", target="id") ^

If I change DateRule’s id field to be public, the compiler next errors out: DateRuleDTOMapper.java:18: error: Unknown property "id" in result type DateRuleDTO. Did you mean "null"? @Mapping(source="id", target="id") ^

I am trying to find a solution that does not require me to write a custom mapper for each record that implements my DateRuleDTO interface, because that would defeat the purpose of using mapstruct’s annotations in the first place. I have found a way to more or less accomplish this using a decorator with (imho) ugly reflection. I would like to see if I can get the result I am looking for (automatic mapping of each record’s id field) with annotations or minimal additional customization.

I should also mention that I am using lombok as well, and have included the lombok-mapstruct-binding dependency in my gradle build file:

    implementation "org.mapstruct:mapstruct:1.5.5.Final"
    annotationProcessor "org.mapstruct:mapstruct-processor:1.5.5.Final"
    // https://mvnrepository.com/artifact/org.projectlombok/lombok-mapstruct-binding
    implementation group: 'org.projectlombok', name: 'lombok-mapstruct-binding', version: '0.2.0'
    annotationProcessor(group: 'org.projectlombok', name: 'lombok-mapstruct-binding', version: '0.2.0')
    // If you are using mapstruct in test code
    testAnnotationProcessor "org.mapstruct:mapstruct-processor:1.5.5.Final"

Thank you for any help!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật