title: Hibernate @EmbeddedId
body: <p>Here is my problem</p>
  
  <p><code>IdComposite</code> class:</p>
  
  <pre><code>@Embeddable
  public class IdComposite implements Serializable{
  
   @Column(name = "code1", nullable = false)
   private String code1;
  
   @Column(name = "code2", nullable = false)
   private String code2;
  
  // getters, setters, hashCode &amp; equals
  
  }
  </code></pre>
  
  <p><code>MyEntity</code> class:</p>
  
  <pre><code>@Entity
  @Table(name = "table")
  public class MyEntity {
  
   @EmbeddedId
   private IdComposite comId;
  
   @ManyToOne(fetch = FetchType.LAZY, cascade = {})
   @JoinColumn(name = "a_id", nullable = false, updatable = false, insertable = false)
   private A a;
  
   // constructors, getters setters, e t.c.
  
  }
  </code></pre>
  
  <p><code>DaoClass</code> fragment:</p>
  
  <pre><code>MyEntity myEntity = new MyEntity();
  A a = new A();
  a.setId(5);
  myEntity.setA(a);
  IdComposite id = new IdComposite();
  id.setCode1("1");
  id.setCode2("2");
  myEntity.setComId(id);
  getSession().save(myEntity);
  </code></pre>
  
  <p>Hibernate result:</p>
  
  <blockquote>
    <p>Hibernate: insert into table (code1, code2) values (?, ?) SQL Error:
    1400, SQLState: 23000 ORA-01400: cannot insert NULL into
    "table"."a_id")</p>
  </blockquote>
  
  <p>What is wrong?</p>
  
  <p><strong>Edited:</strong></p>
  
  <p>Ok, I've changed the code in that way:</p>
  
  <p><code>MyEntity</code> class:</p>
  
  <pre><code>@Entity
  @Table(name = "table")
  public class MyEntity {
  
   @EmbeddedId
   private IdComposite comId;
  
   @Column(name = "a_id", nullable = false, updatable = false, insertable = false)
   private Long aId;
  
   // constructors, getters setters, e t.c.
  
  }
  </code></pre>
  
  <p><code>DaoClass</code> fragment:</p>
  
  <pre><code>MyEntity myEntity = new MyEntity();
  Long aId = 5L;
  A a = (A) getSession().get(A.class, aId);
  if (a == null || a.getId() != 5) { return }
  myEntity.setAId(a.getId());
  IdComposite id = new IdComposite();
  id.setCode1("1");
  id.setCode2("2");
  myEntity.setComId(id);
  getSession().save(myEntity);
  </code></pre>
  
  <p>Hibernate result:</p>
  
  <blockquote>
    <p>Hibernate: insert into table (code1, code2) values (?, ?)
    SQL Error: 1400, SQLState: 23000
    ORA-01400: cannot insert NULL into ("TABLE"."A_ID")</p>
  </blockquote>
  
  <p><strong>Solved</strong></p>
  
  <p>The problem was here:</p>
  
  <pre><code>, insertable = false)
  </code></pre>
  
  <p>So, hibernate generate sql without a_id field that caused error</p>