java JPA OneToOne 双向。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14746318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA OneToOne bidirectional .
提问by Ye Kyaw Kyaw Htoo
I have two entity classes that are in @OneToOne relation. The example code are as follow:
我有两个处于 @OneToOne 关系中的实体类。示例代码如下:
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
}
my question here is "Can I use setA(A a) method in class B. I mean like this . .
我的问题是“我可以在 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();
When I tried like this, the foreign_key field in table A (B_ID) was saved as null.
Please help me.
当我这样尝试时,表A(B_ID)中的foreign_key 字段被保存为空值。
请帮我。
回答by Priyank Doshi
Here , you have specified mappedBy
in class B above private A a;
. In a bidirectionalrelationship , mappedBy
means that I am not the owner. So It means that A is the owner of the relationship.
在这里,您已mappedBy
在上面的 B 类中指定private A a;
。在双向关系中,mappedBy
意味着我不是所有者。所以这意味着A是关系的所有者。
In table of A , you will have a foreignkey for table of B. As A is the owner, A is suppose to cascade operations to B. Ideally you should try a.setB()
and then persist a.
在 A 表中,您将有 B 表的外键。由于 A 是所有者,因此 A 假设将操作级联到 B。理想情况下,您应该尝试a.setB()
然后持久化 a。
Try below:
试试下面:
em.getTransaction().begin();
//first create B.
B bb = new B();
bb.setId(1);
bb.setName("CC");
em.persist(bb);
//create A with B set in it.
A aa = new A();
aa.setId(1);
aa.setName("JJ");
aa.setB(bb);
em.persist(aa);
em.getTransaction().commit();
Or
或者
em.getTransaction().begin();
//first create B.
B bb = new B();
bb.setId(1);
bb.setName("CC");
// no need to persist bb.
//create A with B set in it.
A aa = new A();
aa.setId(1);
aa.setName("JJ");
aa.setB(bb);
em.persist(aa); // because of cascade all , when you persist A ,
// B will also be persisted.
em.getTransaction().commit();
回答by Sudhakar
Use @Cascade({CascadeType.SAVE_UPDATE}) to cascade changes
使用@Cascade({CascadeType.SAVE_UPDATE}) 级联更改
public class B {
@Id
private int id;
private String name;
@OneToOne(mappedBy="b")
@Cascade({CascadeType.SAVE_UPDATE})
private A a;
//setter and getters
}
回答by aman maharjan
you need to add aa.setB(bb) before em.persist(bb)
你需要在 em.persist(bb) 之前添加 aa.setB(bb)
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");
aa.setB(bb);//this line should be added
bb.setA(aa);
em.persist(bb);
em.getTransaction().commit();