JPA

Understanding the mappedBy Attribute and Owning Relationships in JPA

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:

  1. 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.
  2. 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.
  3. 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:

  1. 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.
  2. Owning Side: The entity that owns the relationship and is responsible for persisting and managing the foreign key column(s) in the database.
  3. 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.
  4. 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.

Avatar

Neelabh

About Author

As Neelabh Singh, I am a Senior Software Engineer with 6.6 years of experience, specializing in Java technologies, Microservices, AWS, Algorithms, and Data Structures. I am also a technology blogger and an active participant in several online coding communities.

You may also like

Blog JPA

Transitioning from Java Persistence API (JPA) to Jakarta Persistence: A Comprehensive Guide

What is Jakarta Persistence? This is because the Java Persistence API (JPA) was renamed to Jakarta Persistence in 2019¹. The
Blog JPA

Understanding JPA and Hibernate in Spring Boot Applications

Introduction In this blog post, we will explore two key technologies used in Java applications for interacting with databases: the