In the world of Java Persistence API (JPA), the mappedBy
attribute plays a crucial role when defining bidirectional relationships between entities. To fully grasp the importance of mappedBy
, we must first understand the concept of owning a relationship and related terms.
Owning the Relationship
In a bidirectional relationship, both entities have a reference to each other, but only one entity can be responsible for persisting and managing the relationship in the database. This entity is known as the “owning” or “controlling” entity of the relationship.
The owning entity has the following responsibilities:
- Persistence of the Relationship: The owning entity’s table will contain the foreign key column(s) that reference the other entity. JPA will use these foreign key column(s) to persist and manage the relationship in the database.
- Lifecycle Management: Any changes to the relationship, such as adding, removing, or updating the associated entities, are handled through the owning entity. For example, if you remove an entity from the owning entity’s collection, JPA will automatically remove the corresponding row(s) from the database based on the foreign key relationship.
- Cascade Operations: The owning entity typically dictates how operations like persist, merge, remove, and refresh should cascade to the associated entities.
The non-owning entity (the “inverse” side) is simply a reference to the owning entity and does not directly participate in persisting or managing the relationship in the database.
Related Terms
To better understand the mappedBy
attribute, let’s define some related terms:
- Bidirectional Relationship: A relationship where both entities have a reference to each other. This is in contrast to a unidirectional relationship, where only one entity has a reference to the other.
- Owning Side: The entity that owns the relationship and is responsible for persisting and managing the foreign key column(s) in the database.
- Inverse Side: The non-owning entity in a bidirectional relationship. It is a reference to the owning entity and does not directly manage the relationship in the database.
- Join Column: The column in the owning entity’s table that stores the foreign key reference(s) to the other entity.
The mappedBy
Attribute
The mappedBy
attribute is used in the @OneToMany
or @ManyToMany
annotation on the inverse side of a bidirectional relationship. Its purpose is to specify the field or property in the owning entity that represents the relationship.
By setting the mappedBy
attribute correctly, you tell JPA which entity owns the relationship, and it can manage the persistence and lifecycle of the relationship accurately, avoiding issues like duplicate data or orphaned records in the database.
Here’s an example to illustrate the usage of mappedBy
:
@Entity
public class Department {
@Id
private Long id;
@OneToMany(mappedBy = "department")
private List<Employee> employees;
}
@Entity
public class Employee {
@Id
private Long id;
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
}
In this example, the Employee
entity owns the relationship because it has the @JoinColumn
annotation specifying the department_id
foreign key column. The mappedBy = "department"
in the @OneToMany
annotation on the Department
entity tells JPA that the Employee
entity owns the relationship through the department
field.
By correctly specifying the mappedBy
attribute and understanding the concept of owning the relationship, you can ensure that your JPA entities accurately represent and persist bidirectional relationships in the database.