inverse=true in JPA annotations

前端 未结 3 524
执笔经年
执笔经年 2020-11-30 10:02

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

相关标签:
3条回答
  • 2020-11-30 10:25

    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.

    0 讨论(0)
  • 2020-11-30 10:31

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

    0 讨论(0)
  • 2020-11-30 10:33

    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
    }
    
    0 讨论(0)
提交回复
热议问题