我在Hibernate實(shí)體中有此映射。A.java@OneToMany(mappedBy = "a", cascade = CascadeType.ALL)Set<B> bs;B.java@ManyToOne(cascade = CascadeType.ALL)@JoinColumn(name = "A_ID")A a;我需要對(duì)A進(jìn)行編輯。因此,在以帶注釋的方法加載實(shí)體后 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);上面的代碼是單個(gè)方法的一部分。我得到-刪除的實(shí)例傳遞給合并。請(qǐng)?zhí)岢鼋ㄗh。
1 回答

阿波羅的戰(zhàn)車
TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
您正在cascade = CascadeType.ALL
課堂上使用B
。因此,在執(zhí)行entityManager.remove(b)
刪除操作時(shí),是級(jí)聯(lián)和刪除的a
。
您可以B
根據(jù)需要執(zhí)行以下操作:
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
添加回答
舉報(bào)
0/150
提交
取消