我有 2 个实体和一个嵌入式对象。为了保持讨论的通用性,我将称它们为 Project、ProjectAssociation(嵌入式对象)、员工。
员工使用项目关联与项目相关联。
我的业务要求是我应该能够从员工那里查找 projectAssociation。此外,未来我们可以期待 projectAssociation 获得更多属性。projectAssociation 的生命周期将始终与项目相关联。
Project 和员工实体之间通过 projectAssociation 嵌入对象具有多对多关系。
我如何配置从项目到项目关联的映射。?
Employee {
Project projects;
@OneToMany(mappedBy="What to set here?")
public Set<Project> getProjects() {
return projects;
}
public void setProjects(Set<Project> projects) {
this.projects= projects;
}
}
Project {
Set<ProjectAssociation> projectAssocs;
@Embedded
@ElementCollection
public Set<ProjectAssociation> getProjectAssociation() {
return projectAssocs;
}
public void setProjectAssociation(Set<ProjectAssociation> hostRecovery) {
this.projectAssocs = projectAssocs;
}
}
ProjectAssociation {
// Parent association variable and methods are omitted for brevity
Employee employee;
@ManyToOne
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}