【问题标题】:Mapstruct mapper with inheritence具有继承的 Mapstruct 映射器
【发布时间】:2020-05-26 21:53:42
【问题描述】:

我有一个 BaseEntity,它有一个名为 Customer 的孩子 当我像这样制作映射器时:

@Mapper(componentModel = "spring")
public interface CustomerMapper {

    Customer toEntity(CustomerDTO model);

    List<Customer> toEntityList(List<CustomerDTO> models);

    CustomerDTO toModel(Customer entity);

    List<CustomerDTO> toModelList(List<Customer> entities);

}

BaseEntity 字段不会被 Mapstruct 自动映射。你能告诉我怎么做吗?

编辑:

基础实体:

@MappedSuperclass
@Data
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity implements Serializable {

    private static final long serialVersionUID = 698399842496839094L;

    @EqualsAndHashCode.Include
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    protected Long id;


    @Column(nullable = false, length = 36)
    protected String uuid;
}

客户:

@EqualsAndHashCode(callSuper = true)
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Data
publi class Customer extends BaseEntity {

    private static final long serialVersionUID = 7054669140361044232L;


    @Column(length = 50, unique = true, nullable = false)
    private String login;


    @Column(name = "password_hash", nullable = false, length = 60)
    private String password;
}

【问题讨论】:

  • 这应该可以。您能否将“BaseEntity”、“Customer”和“CustomerDTO”类的源代码添加到问题中?
  • @Sudhir 请检查我的更新
  • CustomerDTOiduuid 字段吗?
  • @MarcoLucidi 是的,相同的类型和相同的名称

标签: java spring mapstruct


【解决方案1】:

我创建了一个sample repo 来证明一切都按预期工作。请将此与您的代码进行比较,看看有什么区别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-23
    • 2012-12-08
    • 2019-07-05
    • 1970-01-01
    • 2019-04-02
    • 2016-01-25
    • 2017-02-15
    • 1970-01-01
    相关资源
    最近更新 更多