在我的应用程序中,我使用JPA2.0和Hibernate作为持久性提供者。我在两个实体之间有一个一对多的关系(使用@JoinColumn而不是@JoinTable)。我想知道如何在JPA注解中指定inverse=true (在hbm.xml中指定)来反转关系所有者。
谢谢。
发布于 2011-02-02 23:59:34
我找到了一个答案。@OneToMany注释的mappedBy属性的行为与xml文件中的inverse = true相同。
发布于 2017-01-09 14:48:18
属性mappedBy表明这一端的实体是关系的反面,而所有者驻留在另一实体中。其他实体将具有@JoinColumn注释和@ManyToOne关系。因此,我认为inverse = true与@ManyToOne注释相同。
此外,inverse=“true”表示这是处理关系的关系所有者。
发布于 2020-07-13 02:46:11
通过使用@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
}https://stackoverflow.com/questions/4865285
复制相似问题