Home > Java > javaTutorial > body text

What is the role of `mappedBy` in JPA and Hibernate for managing bi-directional relationships?

Patricia Arquette
Release: 2024-11-02 04:45:02
Original
518 people have browsed it

What is the role of `mappedBy` in JPA and Hibernate for managing bi-directional relationships?

Understanding mappedBy in JPA and Hibernate

In Java Persistence API (JPA) and Hibernate, mappedBy is a crucial annotation that facilitates the management of bi-directional relationships between entities. It serves two primary purposes:

1. Indicating Inverse Side of a Relationship:

mappedBy identifies the entity that does not own the relationship. It specifies which property in that entity should be used to access the related attribute. This is particularly useful in bi-directional relationships where both entities reference each other.

2. Eliminating Foreign Key Constraint:

Unlike the traditional approach to creating foreign key relationships, mappedBy allows you to define the relationship without physically adding a foreign key column. Instead, it leverages the existence of an existing foreign key on the other entity.

Using mappedBy in Bi-directional Relationships:

To illustrate the usage of mappedBy, consider the example provided:

Airline.java:

<code class="java">@Entity
@Table(name="Airline")
public class Airline {
    // ... Other fields ...
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "IDAIRLINE")
    private Set<AirlineFlight> airlineFlights;
}</code>
Copy after login

AirlineFlight.java:

<code class="java">@Entity
@Table(name="AirlineFlight")
public class AirlineFlight {
    // ... Other fields ...
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "IDAIRLINE")
    private Airline airline;
}</code>
Copy after login

In this example, Airline is the owning entity, as it owns the airlineFlights collection. The relationship from Airline to AirlineFlight is via the airlineFlights property.

On the AirlineFlight side, the airline property is annotated with @ManyToOne and mappedBy = "airlineFlights". This specifies that AirlineFlight is the non-owning side of the relationship, and the airline property is mapped by the airlineFlights property in Airline.

In the database schema provided, AirlineFlights has the IDAIRLINE column as a foreign key to Airline. Therefore, Airline is indeed the owner of the airlineFlights entity.

By using mappedBy, you establish a bi-directional relationship without adding an additional foreign key column to AirlineFlight. Instead, JPA and Hibernate will automatically link the entities based on the specified property mappings.

The above is the detailed content of What is the role of `mappedBy` in JPA and Hibernate for managing bi-directional relationships?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template