我有一個表A,表AB,表B,表AB具有對錶A和表B的外鍵引用。表A和表AB之間存在一對一關係,並且多對一表B和表AB。JPA註釋問題
我的問題是如果我爲表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;
}
那麼你認爲我需要在關係的兩邊加上級聯嗎? – user841293
理想情況下,它應該足以設置在擁有一方的級聯。但是,無論如何都要連接關係的兩邊。 –