我有两个实体类,它们之间存在@OneToOne关系。示例代码如下:
我的问题是“我能在类B中使用setA(A a)方法吗?我的意思是像这样…”
当我尝试这样做时,表A中的外键字段(B_ID)保存为null。
请帮助我。
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
}
我的问题是“我能在类B中使用setA(A a)方法吗?我的意思是像这样…”
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();
当我尝试这样做时,表A中的外键字段(B_ID)保存为null。
请帮助我。