我的公司实体有一组分支机构。当我将实体转换为 DTO 时,我还想将一组分支转换为一组BranchDTO
使用模型映射器。
我的实体:
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "company_id")
private long companyId;
@Column(name = "company_name")
private String companyName;
@Column(name = "address")
private String address;
@OneToMany
@JoinColumn(name="branchcompany_id", referencedColumnName="company_id")
private Set<Branch> branches;
...getters setters..
}
public class Branch {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "branch_id")
private long branchId;
@Column(name = "branch_name")
private String branchName;
...
}
我想将此实体CompanyDTO
与集合一起转移到BranchDTO
我的 DTO:
public class CompanyDTO {
private long companyId;
private String companyName;
private String address;
private List<BranchDTO> branches;
...
}
public class BranchDTO {
private long branchId;
private String branchName;
...
}
我尝试使用属性映射 - 但无法弄清楚确切的语法。以下给出了空白BranchDTO
列表。
CompanyDTO newCompanyDTO = modelMapper.map(company, CompanyDTO.class);
任何帮助是极大的赞赏。