I am currently experiencing a problem that is very similar to the one discussed here Hibernate triggering constraint violations using orphanRemoval
Unfortunately I get a SqlException - Column cannot be null error. My entities are as follows:
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;
My JUnit test is as follows:
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);
It saves correctly. However when I try to clear the set of callCycles, it fails with the Notnull constraint. How can I fix this?