写在开头
使用jhipster声明的OneToMany在One的一方DTO中是没有与Many的DTO的映射关系的, 为了在One的一方DTO中使用Many的DTO, 使用以下三步解决此问题。
步骤
1. OneDTO 中的"mark 1"处为自己写的一对多的关系, 此处变量名称不能与实体One中相应的变量名称一致,否则编译失败。
2. OneMapper 中的"mark 2"处 uses属性添加ManyMapper。
2. OneMapper 中的"mark 3"处使用@Mapping注解声明 Entity 转 DTO 的映射关系。
Entity
@Entity@Table(name = "one")public class One {
... @OneToMany(mappedBy = "one") private Set<Many> manys = new HashSet<>();
... public void setManys(Set<Many> manys) {
this.manys = manys;
}
public Set<Many> getManys() {
return manys;
}
}
@Entity@Table(name = "many")public class Many {
... @ManyToOne private One one;
}
DTO
public class OneDTO {
... // mark 1 private Set<ManyDTO> manyDTOS = new HashSet<>();
... public void setManyDTOS(Set<ManyDTO> manyDTOS) {
this.manyDTOS = manyDTOS;
}
public Set<ManyDTO> getManyDTOS() {
return manyDTOS;
}
}
public class ManyDTO {
... private Long oneId;
... public void setOneId(Long oneId) {
this.oneId = oneId;
}
public Long getOneId() {
return oneId;
}
}
Mapper
// mark 2@Mapper(componentModel = "spring", uses = {
ManyMapper.class}
)public interface OneMapper extends EntityMapper<OneDTO, One> {
// mark 3 @Mapping(souce = "manys", target = "manyDTOS") OneDTO toDto(One one);
... }
@mapper(componentModel = "spring", uses = {
OneMapper.class}
)public interface ManyMapper extends EntityMapper<ManyDTO, Many>{
... }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。