title: Can someone explain mappedBy in JPA and Hibernate?
body: <p>I am new to hibernate and need to use one-to-many and many-to-one relations. It is a bi-directional relationship in my objects, so that I can traverse from either direction. <code>mappedBy</code> is the recommended way to go about it, however, I couldn't understand it. Can someone explain:</p>
  
  <ul>
  <li>what is the recommended way to use it?</li>
  <li>what purpose does it solve?</li>
  </ul>
  
  <p>For the sake of my example, here are my classes with annotations:</p>
  
  <ul>
  <li><code>Airline</code> <strong>OWNS many</strong> <code>AirlineFlights</code></li>
  <li><strong>Many</strong> <code>AirlineFlights</code> belong to <strong>ONE</strong> <code>Airline</code></li>
  </ul>
  
  <p><strong>Airline</strong>:</p>
  
  <pre><code>@Entity 
  @Table(name="Airline")
  public class Airline {
      private Integer idAirline;
      private String name;
  
      private String code;
  
      private String aliasName;
      private Set&lt;AirlineFlight&gt; airlineFlights = new HashSet&lt;AirlineFlight&gt;(0);
  
      public Airline(){}
  
      public Airline(String name, String code, String aliasName, Set&lt;AirlineFlight&gt; flights) {
          setName(name);
          setCode(code);
          setAliasName(aliasName);
          setAirlineFlights(flights);
      }
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name="IDAIRLINE", nullable=false)
      public Integer getIdAirline() {
          return idAirline;
      }
  
      private void setIdAirline(Integer idAirline) {
          this.idAirline = idAirline;
      }
  
      @Column(name="NAME", nullable=false)
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = DAOUtil.convertToDBString(name);
      }
  
      @Column(name="CODE", nullable=false, length=3)
      public String getCode() {
          return code;
      }
      public void setCode(String code) {
          this.code = DAOUtil.convertToDBString(code);
      }
  
      @Column(name="ALIAS", nullable=true)
      public String getAliasName() {
          return aliasName;
      }
      public void setAliasName(String aliasName) {
          if(aliasName != null)
              this.aliasName = DAOUtil.convertToDBString(aliasName);
      }
  
      @OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
      @JoinColumn(name="IDAIRLINE")
      public Set&lt;AirlineFlight&gt; getAirlineFlights() {
          return airlineFlights;
      }
  
      public void setAirlineFlights(Set&lt;AirlineFlight&gt; flights) {
          this.airlineFlights = flights;
      }
  }
  </code></pre>
  
  <p><strong>AirlineFlights:</strong></p>
  
  <pre><code>@Entity
  @Table(name="AirlineFlight")
  public class AirlineFlight {
      private Integer idAirlineFlight;
      private Airline airline;
      private String flightNumber;
  
      public AirlineFlight(){}
  
      public AirlineFlight(Airline airline, String flightNumber) {
          setAirline(airline);
          setFlightNumber(flightNumber);
      }
  
      @Id
      @GeneratedValue(generator="identity")
      @GenericGenerator(name="identity", strategy="identity")
      @Column(name="IDAIRLINEFLIGHT", nullable=false)
      public Integer getIdAirlineFlight() {
          return idAirlineFlight;
      }
      private void setIdAirlineFlight(Integer idAirlineFlight) {
          this.idAirlineFlight = idAirlineFlight;
      }
  
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name="IDAIRLINE", nullable=false)
      public Airline getAirline() {
          return airline;
      }
      public void setAirline(Airline airline) {
          this.airline = airline;
      }
  
      @Column(name="FLIGHTNUMBER", nullable=false)
      public String getFlightNumber() {
          return flightNumber;
      }
      public void setFlightNumber(String flightNumber) {
          this.flightNumber = DAOUtil.convertToDBString(flightNumber);
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Database schema:</p>
  
  <p>AirlineFlights has the idAirline as ForeignKey and Airline has no idAirlineFlights. This makes, AirlineFlights as the owner/identifying entity ?</p>
  
  <p>Theoretically, I would like airline to be the owner of airlineFlights.</p>
  
  <p><img src="https://i.stack.imgur.com/gQvDs.png" alt="enter image description here"></p>