如何在Mapper中删除警告未映射的目标属性?

时间:2019-12-12 11:17:51

标签: java spring spring-data-jpa jhipster mapstruct

自从我的项目从jhipster 5.8.2升级到jhipster 6.5.1以来,Mapper发出了很多警告。

我会正确处理此警告,而不是在所有Mapper中添加此属性:

(unmappedTargetPolicy = ReportingPolicy.IGNORE)

例如,我遇到此错误:

service\mapper\PermissionMapper.java:25: warning: Unmapped target property: "removeEntite".
Permission toEntity(PermissionDTO permissionDTO);

在我的对象Permission中,我有:

@Entity
@Table(name = "permission")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Permission implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
private Profil profil;

@ManyToOne
private User user;

@ManyToMany
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JoinTable(name = "permission_entite",
           joinColumns = @JoinColumn(name = "permissions_id", referencedColumnName = "id"),
           inverseJoinColumns = @JoinColumn(name = "entites_id", referencedColumnName = "id"))
private Set<Entite> entites = new HashSet<>();

.... 
 public Set<Entite> getEntites() {
    return entites;
}

public Permission entites(Set<Entite> entites) {
    this.entites = entites;
    return this;
}

public Permission addEntite(Entite entite) {
    this.entites.add(entite);
    entite.getPermissions().add(this);
    return this;
}

public Permission removeEntite(Entite entite) {
    this.entites.remove(entite);
    entite.getPermissions().remove(this);
    return this;
}

public void setEntites(Set<Entite> entites) {
    this.entites = entites;
}

和PermissionDTO:

public class PermissionDTO implements Serializable {

private Long id;

private Long profilId;

private String profilNom;

private Long userId;

private String userLogin;

private Set<EntiteDTO> entites = new HashSet<>();

... 

public Set<EntiteDTO> getEntites() {
    return entites;
}

public void setEntites(Set<EntiteDTO> entites) {
    this.entites = entites;
}

映射器:

@Mapper(componentModel = "spring", uses = {ProfilMapper.class, UserMapper.class, EntiteMapper.class, })
public interface PermissionMapper extends EntityMapper <PermissionDTO, Permission> {

@Mapping(source = "profil.id", target = "profilId")
@Mapping(source = "profil.nom", target = "profilNom")

@Mapping(source = "user.id", target = "userId")
@Mapping(source = "user.login", target = "userLogin")
PermissionDTO toDto(Permission permission);

@Mapping(source = "profilId", target = "profil")

@Mapping(source = "userId", target = "user")
@Mapping(target = "entites", ignore = true)
Permission toEntity(PermissionDTO permissionDTO);

带有“ ignore = true”的ligne不起作用。

请问您有个主意吗?

1 个答案:

答案 0 :(得分:1)

@Mapper(componentModel = "spring", uses = {ProfilMapper.class, UserMapper.class, EntiteMapper.class, }, unmappedTargetPolicy = ReportingPolicy.IGNORE)

您可以在每个映射器上定义属性,也可以使用共享的映射器配置。 您的问题的解决方案在下面的链接中定义

Ignore unmapped properties