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
}