我有一个名为 employee 的生成表,作为两个表之间的关系:
人, 的 MedicalCompany
我想知道如何在这个生成的表和另一个表之间建立manyToMany关系,如:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "employee_role", joinColumns = { @JoinColumn(name = "employee_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
private Set<Role> roles = new HashSet<Role>(0);
1- 生成表:
@SuppressWarnings("serial")
@Entity
@Table(name = "employee")
@AssociationOverrides(value = {
@AssociationOverride(name = "pk.medicalCompany", joinColumns = @JoinColumn(name = "company_id", referencedColumnName = "id")),
@AssociationOverride(name = "pk.person", joinColumns = @JoinColumn(name = "employee_id", referencedColumnName = "person_id")),
@AssociationOverride(name = "pk.titleText"),
@AssociationOverride(name = "pk.employeeManager") })
public class Employee implements Serializable {
@EmbeddedId
private EmployeeCompanyId pk = new EmployeeCompanyId();
@Transient
public void setEmployeeManager(long employeeManager) {
this.pk.setEmployeeManager(employeeManager);
}
public long getEmployeeManager() {
return pk.getEmployeeManager();
}
@Transient
public void setTitleText(String titleText) {
this.pk.setTitleText(titleText);
}
public String getTitleText() {
return pk.getTitleText();
}
public void setPerson(Person person) {
this.pk.setPerson(person);
}
@Transient
public Person getPerson() {
return this.pk.getPerson();
}
public void setMedicalCompany(MedicalCompany medicalCompany) {
this.pk.setMedicalCompany(medicalCompany);
}
@Transient
public MedicalCompany getMedicalCompany() {
return this.pk.getMedicalCompany();
}
public void setPk(EmployeeCompanyId pk) {
this.pk = pk;
}
public EmployeeCompanyId getPk() {
return pk;
}
}
2- EmployeeCompanyId :
@SuppressWarnings("serial")
@Embeddable
public class EmployeeCompanyId implements Serializable {
@ManyToOne
private Person person;
@ManyToOne
private MedicalCompany medicalCompany;
@Size(max = 150, message = "{long.value}")
@Column(name = "title_text", length = 150, nullable = true)
private String titleText;
@Column(name = "employee_manager")
private long employeeManager;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public MedicalCompany getMedicalCompany() {
return medicalCompany;
}
public void setMedicalCompany(MedicalCompany medicalCompany) {
this.medicalCompany = medicalCompany;
}
public String getTitleText() {
return titleText;
}
public void setTitleText(String titleText) {
this.titleText = titleText;
}
public long getEmployeeManager() {
return employeeManager;
}
public void setEmployeeManager(long employeeManager) {
this.employeeManager = employeeManager;
}
}
答案 0 :(得分:1)
您不能在@Embeddable
内@EmbeddedId
之间建立关系。您只能在常规@Embeddable
中建立关系。
JPA 2.0 FR规范, 11.1.15 EmbeddedId Annotation :
嵌入式id类中定义的关系映射不是 支持的强>
Hibernate 3.5 doc, 2.2.3.2.1。 @EmbeddedId属性
在嵌入式id对象中,关联表示为关联实体的标识符。但您可以通过@MapsId注释将其值链接到实体中的常规关联。
您可以做的最少是在嵌入中使用主键类型。然后,您可以将它们映射到可嵌入存在的实体。换句话说,你可以有类似的东西:
@Embeddable
public class EmployeeCompanyId implements Serializable {
private int personId;
// ...
}
@Entity
public class Employee {
@EmbeddedId
private EmployeeCompanyId pk = new EmployeeCompanyId();
@MapsId("personId")
@ManyToOne
private Person person;
// ...
}