title: Understanding mappedBy annotation in Hibernate
body: <p>I am trying to understand the <code>mappedBy</code> attribute of <code>@OneToMany</code> annotation in JPA. I created below example where a Customer has a list of Orders:</p>
  
  <pre><code>@Entity
  public class Customer {
     @Id @GeneratedValue public Integer getId() { return id; }
     public void setId(Integer id) { this.id = id; }
     private Integer id;
  
     @OneToMany(mappedBy="customer")
     @OrderColumn(name="orders_index")
     public List&lt;Order&gt; getOrders() { return orders; }
     public void setOrders(List&lt;Order&gt; orders) { this.orders = orders; }
     private List&lt;Order&gt; orders;
  }
  
  @Entity
  @Table(name="TBL_ORDER")
  public class Order {
     @Id @GeneratedValue public Integer getId() { return id; }
     public void setId(Integer id) { this.id = id; }
     private Integer id;
  
     public int getOrderNumber() { return orderNumber; }
     public void setOrderNumber(int orderNumber) { this.orderNumber = orderNumber; }
     private int orderNumber;
  
     @ManyToOne
     public Customer getCustomer() { return customer; }
     public void setCustomer(Customer customer) { this.customer = customer; }
     private Customer customer;
  }
  </code></pre>
  
  <p>Now when I use Hibernate to generate the tables then I see that Hibernate created only 2 tables:</p>
  
  <pre><code>Hibernate: create table Customer (id number(10,0) not null, primary key (id))
  Hibernate: create table TBL_ORDER (id number(10,0) not null, orderNumber number(10,0) not null, customer_id number(10,0), orders_index number(10,0), primary key (id))
  Hibernate: alter table TBL_ORDER add constraint FK_nt24krtgqwcsynosqgk4jkvfv foreign key (customer_id) references Customer
  </code></pre>
  
  <p>Also if I try to save a customer and some Orders I see below DML statements generated by Hibernate:</p>
  
  <pre><code>Hibernate: insert into Customer (id) values (?)
  Hibernate: insert into TBL_ORDER (customer_id, orderNumber, id) values (?, ?, ?)
  Hibernate: update TBL_ORDER set orders_index=? where id=?
  </code></pre>
  
  <p>Why hibernate tried to insert and updates the record in TBL_ORDER instead of just running a single insert query?</p>
  
  <p>Now if I remove the mappedBy attribute and try to generate the tables, I see 3 tables this time:</p>
  
  <pre><code>Hibernate: create table Customer (id number(10,0) not null, primary key (id))
  Hibernate: create table Customer_TBL_ORDER (Customer_id number(10,0) not null, orders_id number(10,0) not null, orders_index number(10,0) not null, primary key (Customer_id, orders_index))
  Hibernate: create table TBL_ORDER (id number(10,0) not null, orderNumber number(10,0) not null, customer_id number(10,0), primary key (id))
  Hibernate: alter table Customer_TBL_ORDER add constraint UK_sw94jktvh72tripj876s31052  unique (orders_id)
  Hibernate: alter table Customer_TBL_ORDER add constraint FK_sw94jktvh72tripj876s31052 foreign key (orders_id) references TBL_ORDER
  Hibernate: alter table Customer_TBL_ORDER add constraint FK_f03up2h945cg0dcbo2pdb1d3c foreign key (Customer_id) references Customer
  Hibernate: alter table TBL_ORDER add constraint FK_nt24krtgqwcsynosqgk4jkvfv foreign key (customer_id) references Customer
  </code></pre>
  
  <p>Why hibernate creates an additional table in this case? How the mappedBy attribute is controlling this? Why we have an additional unique key constraint on orders_id column in Customer_TBL_ORDER table?</p>
  
  <p>Now if I try to save a customer and its orders I get below DML operations:</p>
  
  <pre><code>Hibernate: insert into Customer (id) values (?)
  Hibernate: insert into TBL_ORDER (customer_id, orderNumber, id) values (?, ?, ?)
  Hibernate: insert into Customer_TBL_ORDER (Customer_id, orders_index, orders_id) values (?, ?, ?)
  </code></pre>
  
  <p>Why we don't have an additional update query in this case compared to the case where I have declared the mappedBy attribute?</p>