我有以下实体关系:SideA:
@Entity
@Table(name = "SideA")
public class SideA {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@CascadeOnDelete
@OneToMany(fetch = FetchType.LAZY, mappedBy = "sideA", cascade=CascadeType.ALL)
private List<ABAssociation> association = new ArrayList<ABAssociation>();
}
B面:
@Entity
@Table(name = "SideB")
public class SideB {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@CascadeOnDelete
@OneToMany(fetch = FetchType.LAZY, mappedBy = "sideB", cascade=CascadeType.ALL)
private List<ABAssociation> association = new ArrayList<ABAssociation>();
}
ABA协会:
@Entity
@Table(name = "ABAssociation")
public class ABAssociation {
@EmbeddedId
private ABAssociationPK pk = new ABAssociationPK();
@ManyToOne(cascade=CascadeType.MERGE)
@MapsId("aId")
private SideA sideA;
@ManyToOne(cascade=CascadeType.MERGE)
@MapsId("bId")
private SideB sideB;
}
ABA协会PK:
@Embeddable
public class ABAssociationPK implements java.io.Serializable{
private long aId;
private long bId;
}
我的问题是当我删除一侧时,数据库删除了 ABAssociation 中的行,但仍保留在缓存中。
测试代码如下:
SideA a = new SideA();
SideB b = new SideB();
entitymanager.persist(a);
entitymanager.persist(b);
ABAssociation ab = new ABAssociation()
ab.setSideA(a);
ab.setSideB(b);
entitymanager.persist(ab);
a.getABAssociationList().add(ab);
b.getABAssociationList().add(ab);
a = entitymanager.merge(a);
b = entitymanager.merge(b);
entitymanager.delete(a);
由于“a”被删除,“a”和“b”之间的关系也应该被删除。但是当我检查“b.getABAssociationList().size()”时它仍然存在,即使数据库中的 ABAssociation 表中没有行。
这与共享缓存问题有关吗?