title: Foreign key field null in @OneToOne mapping with Hibernate/JPA
body: <p>I have two entities, <code>Document</code> and <code>BodyElement</code>, and trying to persist them with Hibernate 4.2. <code>mtdt_t</code> populates correctly, but the foreign key <code>docid</code> in the <code>mtdt_body_t</code> table is <code>NULL</code>.</p>
  
  <p>I see hibernate trying to insert without the <code>docid</code> value. <code>insert into mtdt_body_t values ( )</code></p>
  
  <pre><code>@Entity
  @Table(name = "mtdt_t")
  public class Document implements Serializable {
  
      @Id  
      @Column(name = "docid", unique = true, nullable = false)
      private String docid;
  
      @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
      @OrderColumn
      @JoinColumn(name = "docid", nullable = false)
      private BodyElement bodyElement;
  
      public String getDocid() {
          return docid;
      }
  
      public void setDocid(String docid) {
          this.docid = docid;
      }
  
      public BodyElement getBodyElement() {
          return bodyElement;
      }
  
      public void setBodyElement(BodyElement bodyElement) {
          this.bodyElement = bodyElement;
      }
  
  }
  
  @Entity
  @Table(name = "mtdt_body_t")
  public class BodyElement implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @OneToOne
      @JoinColumn(name = "docid", insertable = false, updatable = false, nullable = false)
      private Document document;
  
      public BodyElement() {
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public Document getDocument() {
          return document;
      }
  
      public void setDocument(Document document) {
          this.document = document;
      }
  
  }
  </code></pre>
  
  <p>I left off another field. In <code>Document</code> I have,</p>
  
  <pre><code>@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
  @OrderColumn
  @JoinColumn(name = "docid", nullable = false)
  @XmlPath("head/meta/kb:keywords/kb:keyword")
  private Set&lt;Keyword&gt; keywords;
  </code></pre>
  
  <p>In the <code>Keyword</code> class I have the foreign key mapped as,</p>
  
  <pre><code>@ManyToOne
  @JoinColumn(name = "docid", insertable = false, updatable = false, nullable = false)
  @XmlTransient
  private Document document;
  </code></pre>
  
  <p>and that <code>docid</code> field is never <code>NULL</code>.</p>
  
  <p>Is there anything special about the <code>@OneToOne</code> Mapping compared to <code>@OneToMany</code>? I just mimicked what I had done for <code>@OneToMany</code> on the <code>@OneToOne</code> field.</p>
  
  <p>Thanks</p>