我在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 @Transactional
A 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 them
for(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);
上面的代码是单个方法的一部分。
我得到-删除的实例传递给合并。请提出建议。
阿波罗的战车
相关分类