title: How to avoid insert and update queries in one-to-many uni-directional association
body: <p>I have a simple <code>one-to-many</code> relationship between <code>Employee</code> &amp; <code>Phone</code> entities.</p>
  
  <p>I have 2 issues here.</p>
  
  <p><strong>Scenario 1 :</strong></p>
  
  <pre><code>@Entity
  public class Employee {
      @Id
      @GeneratedValue
      private long id;
  
      private String firstName;
      private String lastName;
      private double salary;
  
      @OneToMany
      @JoinColumn(name = "parent_id", referencedColumnName = "id", insertable=false, updatable=false)
      private List&lt;Phone&gt; phones = new ArrayList&lt;Phone&gt;(0);
  
  // Setters &amp; Getter methods
  
  }
  </code></pre>
  
  <p><strong>Phone.java</strong></p>
  
  <pre><code>@Entity
  public class Phone {
      @Id
      private long id;
      private String type;
      private int areaCode;
      private String phoneNumber;
  
  // Setter &amp; Getters
  
  }
  </code></pre>
  
  <p>Now I created employee instance and added 2 phones to it.</p>
  
  <pre><code>Employee e1 = new Employee(10,"Bob", "Way",50000);
  Phone p1 = new Phone(1,"home",613,"792-0000");
  Phone p2 = new Phone(2,"work",613,"896-1234");          
  e1.addPhone(p1);
  e1.addPhone(p2);
  </code></pre>
  
  <p>Then in hibernate, if I use below code:</p>
  
  <pre><code>session.save(e1);
  session.save(p1);
  session.save(p2);
  </code></pre>
  
  <p>or if I save <code>phone</code> then <code>employee</code></p>
  
  <pre><code>session.save(p1);
  session.save(p2);
  session.save(e1);
  </code></pre>
  
  <p>In both cases, Hibernate just runs <code>insert</code> queries with out any <code>update</code> queries but the <code>foreign key</code> is set to NULL.</p>
  
  <pre><code>mysql&gt; select * from Phone;
  +----+----------+-------------+------+-----------+
  | id | areaCode | phoneNumber | type | parent_id |
  +----+----------+-------------+------+-----------+
  |  1 |      613 | 792-0000    | home |      NULL |
  |  2 |      613 | 896-1234    | work |      NULL |
  |  3 |      416 | 123-4444    | work |      NULL |
  +----+----------+-------------+------+-----------+
  </code></pre>
  
  <p>How can I fix this? Please let me know where I made mistake.</p>
  
  <p><strong>Scenario 2 :</strong></p>
  
  <p>Now if I made change to my <code>@JoinColumn</code> in <code>Employee</code> table like below:</p>
  
  <pre><code>@JoinColumn(name = "parent_id")
  </code></pre>
  
  <p>Then Hibernate is generating <code>**insert**</code> then <code>update</code> queries to map the foreign key properly.</p>
  
  <pre><code>Hibernate: insert into Employee (firstName, lastName, salary) values (?, ?, ?)
  Hibernate: insert into Phone (areaCode, phoneNumber, type, id) values (?, ?, ?, ?)
  Hibernate: insert into Phone (areaCode, phoneNumber, type, id) values (?, ?, ?, ?)
  Hibernate: insert into Phone (areaCode, phoneNumber, type, id) values (?, ?, ?, ?)
  Hibernate: update Phone set parent_id=? where id=?
  Hibernate: update Phone set parent_id=? where id=?
  </code></pre>
  
  <p>When Hibernate runs the <code>insert</code> on <code>Employee</code> table then it gets the <code>employee id</code> there itself, then why does Hibernate runs additional <code>update</code> query when it can just run a single insert query on Phone table by including the foreign key value?</p>
  
  <p>I understand that in <code>update</code> query it is adding the foreign key, but I would like to know why this is not managed in <code>insert</code> query itself on Phone table. Can you please explain this?</p>
  
  <p><strong>Scenario 3:</strong></p>
  
  <p>If I say like this:</p>
  
  <pre><code>@JoinColumn(name = "OWNER_ID", insertable=false, updatable=false, nullable=false)
  </code></pre>
  
  <p>Then I get below exception:</p>
  
  <pre><code>Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value : onetomany.Phone._phones_OWNER_IDBackref
  </code></pre>