从父集合中删除时删除子对象

时间:2014-06-23 12:40:33

标签: java hibernate junit jpa-2.0

我目前遇到的问题与此处讨论的问题非常相似 Hibernate triggering constraint violations using orphanRemoval

不幸的是我得到了一个 SqlException - Column不能为null 错误。我的实体如下:

CallDetail

@OneToMany(mappedBy = "callDetail", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<CallCycleDetail> callCycles;

public void addCallCycleDetail(CallCycleDetail callCycleDetail) {
    if (this.callCycles == null) {
        this.callCycles = new HashSet<CallCycleDetail>();
    }
  callCycles.add(callCycleDetail);
  callCycleDetail.setCallDetail(this);
}

public void removeCallCycleDetail(CallCycleDetail callCycleDetail) {
  callCycles.remove(callCycleDetail);
  callCycleDetail.setCallDetail(null);
}

CallCycleDetail

@ManyToOne(optional=false)
@JoinColumn(name = "call_detail", nullable=false)
private CallDetail callDetail;

我的JUnit测试如下:

CallDetail callDetail = createCallDetail();
CallCycleDetail ccd = new CallCycleDetail(callDetail);
callDetail.addCallCycleDetail(ccd);
callDetailService.saveCallDetail(callDetail);

callDetail = callDetailService.findCallDetail(callDetail.getId());
callDetail.removeCallCycleDetail(ccd);
callDetailService.updateCallDetail(callDetail);

正确保存。但是,当我尝试清除callCycles集时,它会失败并带有Notnull约束。我该如何解决这个问题?

0 个答案:

没有答案