title: Hibernate queries for @JoinColumn and mappedBy
body: <p>I am trying to understand the differences in the DML queries generated if I use @JoinColumn compared to mappedBy attribute of @OneToMany mapping in Hibernate.</p>
  
  <p>If I have my java classes defined as:</p>
  
  <pre><code>@Entity
  public class Product {
    @Id
    String serialNumber;
    @OneToMany
    @JoinColumn(name = "PRODUCT_ID")
    Set&lt;Part&gt; parts = new HashSet&lt;Part&gt;();
  }
  
  @Entity
  public class Part {
      @Id
      @GeneratedValue
      int id;
      String partName;
  }
  </code></pre>
  
  <p>Then id I save a Product and Parts then hibernate generates inserts and also update queries:</p>
  
  <pre><code>Hibernate: insert into Product (serialNumber) values (?)
  Hibernate: insert into Part (partName, id) values (?, ?)
  Hibernate: update Part set PRODUCT_ID=? where id=?
  </code></pre>
  
  <p>Now if I have my java classes defined as:</p>
  
  <pre><code>@Entity
  public class Customer {
      @Id
      @GeneratedValue
      private Integer id;
      @OneToMany(mappedBy = "customer")
      private List&lt;Order&gt; orders;
  }
  
  @Entity
  @Table(name="TBL_ORDER")
  public class Order {
      @Id
      @GeneratedValue
      private Integer id;
      private int orderNumber;
      @ManyToOne
      private Customer customer;
  
      @Override
      public String toString() {
          return id.toString();
      }
  }
  </code></pre>
  
  <p>Then Hibernate generates only insert queries without any updates:</p>
  
  <pre><code>Hibernate: insert into Customer (id) values (?)
  Hibernate: insert into TBL_ORDER (customer_id, orderNumber, id) values (?, ?, ?)
  </code></pre>
  
  <p>If my relationship between entities is only one-to-many then why hibernate needs insert &amp; updates for first case and only insert queries in second case? Please explain.</p>