In this tutorial we are going to see, what’s the difference between @JoinColumn and mappedBy in @OneToMany mapping using JPA with Hibernate.
1. Join Column
A join column is column in a database table that is used to link to another table. Let’s see few database tables to understand Join Column. We have BRANCH and STUDENT tables. BRANCH_ID in STUDENT table is a Join column which is used to link between these two tables to make relation between them. Each engineering branch has many students, so, Branch and Student tables has One-To-Many relation.
In STUDENT table BRANCH_ID is foreign key. A join column is need not to be a foreign key. A join column is just used to link with another table. Let’s see another example.
We have USER and CONTACT_ADDRESS tables. Here, USER_ID in CONTACT_ADDRESS table is a Join Column. Which is used to link with USER table.
2. @JoinColumn annotation
In Simple, @JoinColumn is used to map a database join column in entities. @JoinColumn specifies a column for joining an entity association or element collection.
In Student entity mapping, @JoinColumn is used to map BRANCH_ID join column to associate Branch entity.
@Entity public class Student implements Serializable { @Id private int id; @Column(name="CONTACT_NO") private String contactNo; private String fname; private String lname; //bi-directional many-to-one association to Branch @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="BRANCH_ID") private Branch branch; .... }
In following User entity mapping, @JoinColumn is used to map USER_ID join column of Contac_Adress table to associate collection of ContactAddress in User entity.
@Entity(name="USER") public class User { @Id @Column(name = "ID") private Long id; @ElementCollection @CollectionTable(name="CONTACT_ADDRESS", [email protected](name="USER_ID")) @AttributeOverride(name="streetAddress", [email protected](name="STREET_ADDRESS")) private List<ContactAddress> address; ... more }
3. mappedBy attribute
In JPA or Hibernate, entity associations are directional, either unidirectional or bidirectional. Always mappedBy attribute is used in bidirectional association to link with other side of entity.
In the above tables, BRANCH and STUDENT tables has One-To-Many association. In Hibernate association mappings, each entity plays a role of either owning-entity (the entity which is having foreign key mapping of other entity’s mapped table) or non owning entity (the other side of owning entity).
In above One-To-Many relation, Student entity has owning-side role, which is mapped with foreign key of BRANCH table to the branch reference using @JoinColumn. To make the association bidirectional, the other side of entity Branch also should have Student(s) entity reference and we will be used mappedBy attribute to map to student(s) reference in Branch entity.
mappedBy attribute indicates that which entity owns the relationship (in this example, Student) and what reference is used for non-owning entity within owner entity (in this example, branch is the reference name used in Student entity to map Branch entity).
Let’s see the Branch mapping to understand how to use mappedBy.
@Entity public class Branch implements Serializable { @Id @Column(name="BRANCH_ID") private int branchId; @Column(name="BRANCH_NAME") private String branchName; @Column(name="BRANCH_SHORT_NAME") private String branchShortName; private String description; //bi-directional many-to-one association to Student @OneToMany(mappedBy="branch", cascade={CascadeType.ALL}, orphanRemoval = true) private List<Student> students; .... omitted setters getters etc }
4. Conclusion
In this tutorial, we walked through several examples to understand how to use @JoinColumn and mappedBy attribute in @OneToMany relation and to understand differences between them.