Hibernate: delete orphan from Join Table
I have two persistent classes. 开发者_运维技巧They are are in many-to-many relationship. One class have a set of objects of another class.
@Entity class A {
@Id @Generated int id;
@ManyToMany(cascade = CascadeType.ALL)
Set<B> myset = new HashSet<B>();
}
and
@Entity class B {
@Id @Generated int id;
}
Hibernate makes table: A_B with columns A_id and B_id. If i delete some A or B object, any entrie in A_B table makes no sense anymore. Can it be deleted automaticly?
When you delete A, you should clear its set of Bs before.
When you delete B, you should remove it from the set of Bs of every A. This os of course easier if you make the relationship bidirectional.
精彩评论