【发布时间】:2015-04-13 12:33:32
【问题描述】:
我正在尝试使用合并从父实体中删除子实体(不删除数据库中的引用),我所做的是从父实体中获取 @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
【问题讨论】:
标签: java spring jpa merge jpa-2.0