我有以下架构:
@Entity
public class A{
@OneToMany(mappedBy = "dest",cascade = CascadeType.ALL, orphanRemoval = true)
private Collection<C> myDest; //Owns these
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL, orphanRemoval = true)
private Collection<C> myParents; //can be reached from these
@ManyToMany
private Collection<B> myOwnB;;
}
@Entity
public class B{
@OneToMany(mappedBy = "forB",cascade = CascadeType.ALL, orphanRemoval = true)
private Collection<C> associatedC;
@ManyToMany(mappedBy = "myOwnB")
private Collection<A> associatedA;
}
@Entity
public class C{
@JoinColumn(referencedColumnName = "myDest")
@ManyToOne
private A dest;
@JoinColumn(referencedColumnName = "myParents")
@ManyToOne
private A owner;
@JoinColumn(referencedColumnName = "associatedC")
@ManyToOne
private B forB;
}
这里,如果删除A或B的记录,则删除所有关联的C。完美。
所以我的问题是: 如果我从A中删除B的引用(在ManyToMany关系中),我需要删除所有C以及A和B.(所有Cs,其中forB = removedB和owner = sourceA)。我现在写了一个类似的查询,它现在可以正常工作,我在EJB中显式执行。是否存在一个变通方法或注释,如果其中一个属性(列)变为空,则会删除C的记录?