写在开头
使用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 Setmanys = new HashSet<>(); ... public void setManys(Set manys) { this.manys = manys; } public Set getManys() { return manys; }}@Entity@Table(name = "many")public class Many { ... @ManyToOne private One one;}
DTO
public class OneDTO { ... // mark 1 private SetmanyDTOS = new HashSet<>(); ... public void setManyDTOS(Set manyDTOS) { this.manyDTOS = manyDTOS; } public Set 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{ // mark 3 @Mapping(souce = "manys", target = "manyDTOS") OneDTO toDto(One one); ... }@mapper(componentModel = "spring", uses = {OneMapper.class})public interface ManyMapper extends EntityMapper { ... }