【发布时间】: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 请检查我的更新
-
CustomerDTO有id和uuid字段吗? -
@MarcoLucidi 是的,相同的类型和相同的名称