写在开头
使用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
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @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
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // 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>{ ... }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://my.oschina.net/tianshl/blog/1617662