JPA 2.0 EntityManager合并删除引用什么都不做



我正试图使用merge从父实体中删除一个子实体(而不从DB中删除引用),我所做的是从父实体获得@OneToMany字段的集合,然后从集合中删除,最后我使用merge,当我使用相同的方法但添加而不是删除时,这是有效的,实体是:

@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。

最新更新