2

我有 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;
    }
}
4

1 回答 1

0

您需要将其映射为 @ManyToMany 与中间表和外键列,然后设置 Inverse() 以便 Employee 不维护任何链接表条目

@ManyToMany(...)
@Inverse()
public Set<Project> getProjects() {
    return projects;
}
于 2012-05-31T11:27:16.250 回答