JPA 注释中的 inverse=true

发布于 2024-10-15 07:20:00 字数 215 浏览 1 评论 0原文

在我的应用程序中,我使用 JPA 2.0 和 Hibernate 作为持久性提供程序。我在两个实体之间有一对多关系(使用 @JoinColumn 而不是 @JoinTable)。我想知道如何在 JPA 注释中指定 inverse=true (如 hbm.xml 中指定)来反转关系所有者。

谢谢。

In my application I use JPA 2.0 with Hibernate as the persistence provider. I have a one-to-many relationship between two entities (using a @JoinColumn and not @JoinTable). I wanted to know how could I specify inverse=true (as specified in hbm.xml) in JPA annotations to reverse the relationship owner.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尸血腥色 2024-10-22 07:20:01

通过使用@OneToMany@ManyToManyma​​ppedBy属性,我们可以在注释方面启用inverse="true"。
例如 Branch 和 Staff 具有一对多关系

@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
}

by using mappedBy attribute of @OneToMany or @ManyToMany we can enable inverse="true" in terms of annotation.
For example Branch and Staff having one-to-many relationship

@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
}
数理化全能战士 2024-10-22 07:20:00

我找到了这个问题的答案。 @OneToMany 注释的mappedBy 属性的行为与xml 文件中的inverse = true 相同。

I found an answer to this. The mappedBy attribute of @OneToMany annotation behaves the same as inverse = true in the xml file.

动听の歌 2024-10-22 07:20:00

属性mappedBy表示这一方的实体是关系的逆,所有者驻留在另一方实体中。其他实体将具有 @JoinColumn 注释和 @ManyToOne 关系。因此我认为 inverse = true 与 @ManyToOne 注释相同。

另外 inverse=”true” 表示这是处理该关系的关系所有者。

The attribute mappedBy indicates that the entity in this side is the inverse of the relationship, and the owner resides in the other entity. Other entity will be having @JoinColumn annotaion and @ManyToOne relationship. Hence I think inverse = true is same as @ManyToOne annotation.

Also inverse=”true” mean this is the relationship owner to handle the relationship.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文