0

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?

Community
  • 1
  • 1
Neriyan
  • 155
  • 2
  • 2
  • 14
  • 1
    Even if it hurts, one solution is to remove the `nullable=false` of the CallDetail. I encountered this problem once, and it seems, Hibernate first sets the forign key field to null, and wants to delete these orphans later on. – Uwe Allner Jun 23 '14 at 12:44
  • @UweAllner Thank you for the response. I have removed `@JoinColumn(name = "call_detail", nullable=false)`. However the exception is still thrown. – Neriyan Jun 23 '14 at 12:51
  • Hopefully not the whole annotiation? Only the nullable attribute... And I think, you will not need to do `callCycleDetail.setCallDetail(null);`; Hibernate will recognize that the CallDetail has been removed from the collection. – Uwe Allner Jun 23 '14 at 12:58
  • @UweAllner I also had to remove the `(optional=false)` part of the annotation for this to work. It seems like this is indeed a Hibernate error as described in [HHH-6709](https://hibernate.atlassian.net/browse/HHH-6709). Thank You. – Neriyan Jun 23 '14 at 13:16
  • What is the vendor of your database ? – Rafik BELDI Jul 11 '14 at 23:04

0 Answers0