簡體   English   中英

JPA 2.0 EntityManager合並刪除引用不執行任何操作

[英]JPA 2.0 EntityManager merge remove a reference do nothing

我正在嘗試使用合並從父實體中刪除子對象(不從數據庫中刪除引用),我的工作是從父實體中獲取@OneToMany字段的集合,然后從集合中刪除,最后我使用合並,當我使用相同的方法但添加而不是刪除時,這適用於:

@Entity
@Table(name="bills")
public class Bill {

    @OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL,mappedBy="bill")
    private Set<BillDetail> billDetails = new LinkedHashSet<>();

    ...
}

@Entity
@Table(name="billDetails")
public class BillDetail {

    @ManyToOne()
    @JoinColumn(name="bill")
    private Bill bill;

    ...
}

我合並的代碼是:

Collection<Object> references = (Collection<Object>) PropertyUtils.getSimpleProperty(parentEntity, referenceField);
references.remove(childEntity); // the adding are the same, only i change remove for add here
PropertyUtils.setSimpleProperty(parentEntity, referenceField,references);

requirementCheck.setData(childEntity);

entityManager.getTransaction().begin();
entityManager.merge(parentEntity);
entityManager.getTransaction().commit();

entityManager.close(); 

因此,刪除后我看不到任何更新日志,我缺少什么? 我在進行合並的類上使用@Transactional,並且一起使用Aspectj和Spring AOP(確保沒有將AOP和Aspectj應用於同一方法),我也在使用Hibernate

該關系歸BillDetail實例所有,因此對關系的任何更改都將需要更改BillDetail,並且必須合並BillDetail。

它在添加時有效,因為合並能夠在關系上級聯,因此BillDetail中的更改也會合並。 刪除參考時,您將切斷該參考,以防止使用級聯選項來找到BillDetail實例,因此在清除其賬單參考之后,您必須手動對其進行調用merge。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM