eclipselink 2 OneToMany与一个关联表如何从一侧删除 [英] eclipselink 2 OneToMany with one association table how to delete from one side
问题描述
我具有以下实体关系: SideA:
i have the following entity relationship: 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;
}
ABAssociationPK:
ABAssociationPK:
@Embeddable
public class ABAssociationPK implements java.io.Serializable{
private long aId;
private long bId;
}
我的问题是,当我删除一侧时,数据库会删除ABAssociation中的行,但仍保留在缓存中.
my problem is when i delete one side, the database delete the row in ABAssociation , but still stay in cache.
测试代码如下:
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表中也没有行.
Since "a" was deleted, the relationship between "a" and "b" should be deleted too. but when i check the "b.getABAssociationList().size()" it still there, even there is no rows in ABAssociation table in DB.
这与共享缓存问题有关吗?
it this related to the share cache issue ?
推荐答案
在JPA中,您必须维护对象的关系.
In JPA you must maintain you object's relationships.
如果删除对象,则必须首先删除对该对象的所有引用.
If you remove an object, you must first remove all references to it.
这篇关于eclipselink 2 OneToMany与一个关联表如何从一侧删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!