I have 4 objects:
Profile related to companyContract:
@OneToMany(fetch = FetchType.EAGER, mappedBy = "profile", cascade = { CascadeType.ALL }) @Fetch(value = FetchMode.SUBSELECT) @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN }) private List<CompanyContract> companyContracts;
CompanyContract: Schedule Related:
@OneToMany(mappedBy = "companyContract", cascade = { CascadeType.ALL },orphanRemoval = true, fetch = FetchType.EAGER) @Fetch(FetchMode.SUBSELECT) @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN }) private List<Timesheet> timesheets; @ManyToOne @JoinColumn(name = "IDPROFILE") private Profile profile;
Schedule related to account:
@OneToMany(mappedBy = "timesheet", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @Fetch(value = FetchMode.SUBSELECT) @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN }) private List<Invoice> invoices; @ManyToOne @JoinColumn(name = "IDCONTRACT") private CompanyContract companyContract;
Score:
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "ID_TIMESHEET") private Timesheet timesheet;
So, as you can see here, I am using org.hibernate.annotations.CascadeType.DELETE_ORPHAN so that I can remove the children of the parent.
If I do this:
Profile p = companyContract.getProfile(); p.getCompanyContracts().remove(companyContract); companyContract.setProfile(null); profileService.update(p);
---> The removal order should be:
Delete invoices β Schedules β Company Contract, No?
And instead, I get this error:
org.hibernate.exception.ConstraintViolationException: IDCONTRACT column cannot be null
And I checked this error occurs after profileService.updateProfile(p);
java spring mysql hibernate
Moatez bouhdid
source share