title: JPA OneToOne bidirectional .
body: <p>I have two entity classes that are in @OneToOne relation. The example code are as follow: </p>
  
  <pre><code>public class A {
  @Id
  private int id;
  private String name;
  @JoinColumn(name = "B_ID", referencedColumnName = "id")
  @OneToOne(cascade=CascadeType.ALL)
  private B b;
  
  //setters and getters
  
  }
  
  public class B {
  @Id
  private int id;
  private String name;
  @OneToOne(mappedBy="b")
      private A a;
  //setter and getters
  
  }
  </code></pre>
  
  <p>my question here is "Can I use setA(A a) method in class B. I mean like this . .</p>
  
  <pre><code>em.getTransaction().begin();
  A aa = new A();
  aa.setId(1);
  aa.setName("JJ");
  em.persist(aa);
  
  B bb = new B();
  bb.setId(1);
  bb.setName("CC");
  bb.setA(aa);
  em.persist(bb);
  em.getTransaction().commit();
  </code></pre>
  
  <p>When I tried like this, the foreign_key field in table A (B_ID) was saved as null.<br>
  Please help me.</p>