忽略列表中对象的特定属性
Ignore specific proprety of object in list
我有这些对象:
Class 域
public class Partecipation implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
@OneToMany(mappedBy = "partecipation")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Set<DesignatedCompany> designatedCompanies = new HashSet<>();
...
}
我的 DTO
public class PartecipationDTO implements Serializable {
private Long id;
...
private Set<DesignatedCompanyDTO> designatedCompanies = new HashSet<>();
...
}
已连接 DTO
public class DesignatedCompanyDTO implements Serializable {
private Long id;
...
private PartecipationDTO partecipation;
...
}
我有这个映射器:
public interface PartecipationMapper extends EntityMapper<PartecipationDTO, Partecipation> {
...
PartecipationDTO toDto(Partecipation partecipation);
}
正确的是代码出错了,因为它进入了一个循环条件,详细信息在这里:
public class PartecipationMapperImpl implements PartecipationMapper {
...
protected DesignatedCompanyDTO designatedCompanyToDesignatedCompanyDTO(DesignatedCompany designatedCompany) {
if ( designatedCompany == null ) {
return null;
}
DesignatedCompanyDTO designatedCompanyDTO = new DesignatedCompanyDTO();
designatedCompanyDTO.setId( designatedCompany.getId() );
designatedCompanyDTO.setCompanyEopooCode( designatedCompany.getCompanyEopooCode() );
designatedCompanyDTO.setNote( designatedCompany.getNote() );
designatedCompanyDTO.setPartecipation( toDto( designatedCompany.getPartecipation() ) ); // <--- this line cause the error
return designatedCompanyDTO;
}
...
}
是否可以在映射器中设置排除列表中子对象的 属性?例如,像这样:
@Mapping(target = "designatedCompanies[].partecipation", ignore = true)
我已经解决了问题,感谢filiphr (https://github.com/mapstruct/mapstruct/issues/933#issuecomment-265952166)
在2个DTO的Mappers中工作,这里是代码:
@Mapper(componentModel = "spring", uses = {DesignatedCompanyMapper.class})
public interface PartecipationMapper extends EntityMapper<PartecipationDTO, Partecipation> {
...
@Mapping(target = "designatedCompanies", qualifiedByName="NoPartecipation")
PartecipationDTO toDto(Partecipation partecipation);
...
}
以及可迭代对象的Mapper
public interface DesignatedCompanyMapper extends EntityMapper<DesignatedCompanyDTO, DesignatedCompany> {
@Mapping(source = "partecipation.id", target = "partecipationId")
DesignatedCompanyDTO toDto(DesignatedCompany designatedCompany);
...
@Named("NoPartecipation")
@Mapping(source = "partecipation.id", target = "partecipationId")
@Mapping(target = "partecipation", ignore = true)
DesignatedCompanyDTO toDtoNoPartecipation(DesignatedCompany designatedCompany);
}
我有这些对象:
Class 域
public class Partecipation implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
@OneToMany(mappedBy = "partecipation")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Set<DesignatedCompany> designatedCompanies = new HashSet<>();
...
}
我的 DTO
public class PartecipationDTO implements Serializable {
private Long id;
...
private Set<DesignatedCompanyDTO> designatedCompanies = new HashSet<>();
...
}
已连接 DTO
public class DesignatedCompanyDTO implements Serializable {
private Long id;
...
private PartecipationDTO partecipation;
...
}
我有这个映射器:
public interface PartecipationMapper extends EntityMapper<PartecipationDTO, Partecipation> {
...
PartecipationDTO toDto(Partecipation partecipation);
}
正确的是代码出错了,因为它进入了一个循环条件,详细信息在这里:
public class PartecipationMapperImpl implements PartecipationMapper {
...
protected DesignatedCompanyDTO designatedCompanyToDesignatedCompanyDTO(DesignatedCompany designatedCompany) {
if ( designatedCompany == null ) {
return null;
}
DesignatedCompanyDTO designatedCompanyDTO = new DesignatedCompanyDTO();
designatedCompanyDTO.setId( designatedCompany.getId() );
designatedCompanyDTO.setCompanyEopooCode( designatedCompany.getCompanyEopooCode() );
designatedCompanyDTO.setNote( designatedCompany.getNote() );
designatedCompanyDTO.setPartecipation( toDto( designatedCompany.getPartecipation() ) ); // <--- this line cause the error
return designatedCompanyDTO;
}
...
}
是否可以在映射器中设置排除列表中子对象的 属性?例如,像这样:
@Mapping(target = "designatedCompanies[].partecipation", ignore = true)
我已经解决了问题,感谢filiphr (https://github.com/mapstruct/mapstruct/issues/933#issuecomment-265952166)
在2个DTO的Mappers中工作,这里是代码:
@Mapper(componentModel = "spring", uses = {DesignatedCompanyMapper.class})
public interface PartecipationMapper extends EntityMapper<PartecipationDTO, Partecipation> {
...
@Mapping(target = "designatedCompanies", qualifiedByName="NoPartecipation")
PartecipationDTO toDto(Partecipation partecipation);
...
}
以及可迭代对象的Mapper
public interface DesignatedCompanyMapper extends EntityMapper<DesignatedCompanyDTO, DesignatedCompany> {
@Mapping(source = "partecipation.id", target = "partecipationId")
DesignatedCompanyDTO toDto(DesignatedCompany designatedCompany);
...
@Named("NoPartecipation")
@Mapping(source = "partecipation.id", target = "partecipationId")
@Mapping(target = "partecipation", ignore = true)
DesignatedCompanyDTO toDtoNoPartecipation(DesignatedCompany designatedCompany);
}