JPA注解问题
我有一个表 A ,表 AB ,表 B ,表 AB 具有对表 A 和表 B 的外键引用。表 A 和表 AB 之间存在一对一关系。表 B 和表 AB 之间存在多对一关系。
我的问题是,如果我要为表 B 保存域,它会在表 AB 中保存数据,而不是在 A 中。请告诉我这是否是预期行为,或者我可以通过调用对象 B 上的保存来保存所有其他表中的所有数据吗?
Class A
{
@OneToOne(mappedBy="abpk.a")
@Cascade({ org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
private AB ab;
}
@Entity
@AssociationOverrides({
@AssociationOverride(name = "abpk.a", joinColumns = @JoinColumn(name = "colA", referencedColumnName = "colA")),
@AssociationOverride(name = "abpk.b", joinColumns = @JoinColumn(name = "Colb", referencedColumnName = "colB")) })
Class AB
{
ABPK abpk = new ABPK();
A a;
B b;
//inner class
class ABPK
{
A a;
B b;
@OneToOne(fetch = FetchType.LAZY)
@Cascade({ org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
@JoinColumn(name="b")
//Getter for A
GetA();
@ManyToOne(fetch = FetchType.LAZY)
//Getter for B
GetB();
}
}
Class B
{
@OneToMany(fetch = FetchType.LAZY, mappedBy = "abpk.b")
@Cascade({ org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
private List<AB> abList;
}
I have a Table A , Table AB , TABLE B , Table AB has foreign key references to Table A and Table B. There is a One to One relation between Table A and Table AB.and Many to one between Table B and Table AB.
My question is if i am saving domain for table B, it is saving data in table AB but not in A Please let me know if this is the expected behavior or can i save all data in all other tables just by calling save on Object B
Class A
{
@OneToOne(mappedBy="abpk.a")
@Cascade({ org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
private AB ab;
}
@Entity
@AssociationOverrides({
@AssociationOverride(name = "abpk.a", joinColumns = @JoinColumn(name = "colA", referencedColumnName = "colA")),
@AssociationOverride(name = "abpk.b", joinColumns = @JoinColumn(name = "Colb", referencedColumnName = "colB")) })
Class AB
{
ABPK abpk = new ABPK();
A a;
B b;
//inner class
class ABPK
{
A a;
B b;
@OneToOne(fetch = FetchType.LAZY)
@Cascade({ org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
@JoinColumn(name="b")
//Getter for A
GetA();
@ManyToOne(fetch = FetchType.LAZY)
//Getter for B
GetB();
}
}
Class B
{
@OneToMany(fetch = FetchType.LAZY, mappedBy = "abpk.b")
@Cascade({ org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
private List<AB> abList;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您如何设置级联策略。如果您将其设置为 CASCADE_TYPE ALL,那么理论上它应该持续存在。
但无论如何,当你试图维持联系时,最好将关系的双方联系起来。
That depends on how you have set your cascade policies. If you have set it to CASCADE_TYPE ALL, then theoretically it should persists.
But in any case, it is better to wire up both sides of a relationship when you are trying to persist associations.