JPA Annotation issue
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;
}
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.
精彩评论