我在Hibernate实体中有此映射。A.java@OneToMany(mappedBy = "a", cascade = CascadeType.ALL)Set<B> bs;B.java@ManyToOne(cascade = CascadeType.ALL)@JoinColumn(name = "A_ID")A a;我需要对A进行编辑。因此,在以带注释的方法加载实体后 Spring @TransactionalA a = entityManager.find(A.class, a.getId);// set some new values on the instance variables of a.// take out the set of Bs through a and delete themfor(B b : a.getBs()) { entityManager.remove(b);}// create new objects of B and add them to the below set-Set<B> bs = new HashSet<>();a.setBs(bs);entityManager.merge(a);上面的代码是单个方法的一部分。我得到-删除的实例传递给合并。请提出建议。
1 回答
![?](http://img1.sycdn.imooc.com/5333a1660001394602000200-100-100.jpg)
阿波罗的战车
TA贡献1862条经验 获得超6个赞
您正在cascade = CascadeType.ALL
课堂上使用B
。因此,在执行entityManager.remove(b)
删除操作时,是级联和删除的a
。
您可以B
根据需要执行以下操作:
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
添加回答
举报
0/150
提交
取消