我有两个实体类A和B,它们分别以双向一对多关系存在。
A.java:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "aId",
fetch=FetchType.EAGER, orphanRemoval=true)
private Set<B> bCollection = new LinkedHashSet<B>();
B.java
@JoinColumn(name = "A_ID", referencedColumnName = "ID", nullable=false)
@ManyToOne(optional = false)
private A aId;
在一个简单的控制台应用程序中,我从数据库中获取了一个特定的 A行并尝试删除其详细信息B行之一(随机),但JPA/Hibernate不仅删除了该行 - 它甚至不发出任何DELETE语句数据库。删除B行的唯一方法是从A.java的集合 ( LinkedHashSet ) 中删除相应的实体。所以,虽然我有一个解决方法,但我想了解为什么下面的代码会失败,并且也会悄悄地失败!
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testjpa");
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = em.getTransaction();
entityTransaction.begin();
A a = em.find(A.class, 1);
B b = getARandomChildOfA(a);
em.remove(em.merge(b)); // simple em.remove(b) doesn't work either
entityTransaction.commit();
em.close();
entityManagerFactory.close();
}