title: Null foreign key, in ManyToOne relation using hibernate [4.1.1] annotations
body: <p>I am trying to persist a one-to-many and a many-to-one relation using <em>Hibernate 4.1.1</em> but the foreign key is always <em>NULL</em>.</p>
  
  <p>There are two entities: <em>Account</em> and <em>Client</em>. A <em>Client</em> could have multiple <em>Accounts</em> while an <em>Account</em> has exactly one <em>Client</em>.</p>
  
  <p>Here are the classes (only what matters):</p>
  
  <p><strong>Account.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "account")
  public class Account implements Serializable {
      private Client client;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      public long getId() {
          return id;
      }
  
      @ManyToOne
      @JoinColumn(name = "id_client")
      public Client getClient() {
          return client;
      }
  }
  </code></pre>
  
  <p><strong>Client.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "client")
  public class Client implements Serializable {
      private List&lt;Account&gt; accounts;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      public long getId() {
          return id;
      }
  
      @OneToMany(mappedBy = "client", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      public List&lt;Account&gt; getAccounts() {
          return accounts;
      }
  }
  </code></pre>
  
  <p><strong>Test.java</strong></p>
  
  <pre><code>session.beginTransaction();
  
  Client client = new Client();
  Account account1 = new Account();
  Account account2 = new Account();
  
  a.addAccount(account1);
  a.addAccount(account2);
  
  session.save(client);
  session.getTransaction().commit();
  </code></pre>
  
  <p>While running, Hibernate adds the foreign key to the table:</p>
  
  <p><code>Hibernate: alter table account add index FKB9D38A2D3B988D48 (id_client), add constraint FKB9D38A2D3B988D48 foreign key (id_client) references client (id)</code></p>
  
  <p>Both accounts have id_client column <em>NULL</em>.</p>
  
  <p>I tried putting <strong>nullable = false</strong> at the @JoinColumn relation but that just invoked an exception.</p>
  
  <p><code>Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Column 'id_client' cannot be null</code></p>