Written at the beginning
OneToMany declared using jhipster does not have a mapping relationship with Many's DTO in One's DTO. In order to use Many's DTO in One's DTO, the following three steps are used to solve this problem.
step
1. The one-to-many relationship written by itself at "mark 1" in OneDTO. Here the variable name cannot be consistent with the corresponding variable name in the entity One, otherwise the compilation will fail.
2. Add ManyMapper to the uses attribute at "mark 2" in OneMapper.
2. Use the @Mapping annotation to declare the mapping relationship between Entity to DTO at "mark 3" in OneMapper.
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>{ ... }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.