在我的应用程序中,我使用Hibernate作为持久化提供程序的JPA 2.0。我有两个实体之间的一对多关系(使用
谢谢。
@JoinColumn
而不是@JoinTable
)。我想知道如何在JPA注释中指定inverse=true
(如在hbm.xml
中指定),以反转关系所有者。谢谢。
@JoinColumn
而不是@JoinTable
)。我想知道如何在JPA注释中指定inverse=true
(如在hbm.xml
中指定),以反转关系所有者。我找到了答案。@OneToMany注释的mappedBy属性与xml文件中的inverse = true行为相同。
属性mappedBy
表示此侧的实体是关系的反向,所有权属于另一个实体。其他实体将具有@JoinColumn
注释和@ManyToOne
关系。因此,我认为inverse = true与@ManyToOne
注释相同。
另外,inverse = "true"表示这是关系所有者来处理关系。
@OneToMany
或@ManyToMany
的mappedBy
属性,我们可以在注解方面启用inverse="true"
。例如,分支机构和员工之间存在一对多关系。@Entity
@Table(name = "branch")
public class Branch implements Serializable {
@Id
@Column(name = "branch_no")
@GeneratedValue(strategy = GenerationType.AUTO)
protected int branchNo;
@Column(name = "branch_name")
protected String branchName;
@OneToMany(mappedBy = "branch") // this association is mapped by branch attribute of Staff, so ignore this association
protected Set<Staff> staffSet;
// setters and getters
}
@Entity
@Table(name = "staff")
public class Staff implements Serializable {
@Id
@Column(name = "staff_no")
@GeneratedValue(strategy = GenerationType.AUTO)
protected int staffNo;
@Column(name = "full_name")
protected String fullName;
@ManyToOne
@JoinColumn(name = "branch_no", nullable = true)
protected Branch branch;
// setters and getters
}