Delete order in sleep mode

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);

+8
java spring mysql hibernate
source share
2 answers

The problem is that the IDCONTRACT column in the table containing the schedules has a NOT NULL . Delete it and try again.

If you automatically generate a schema, try adding @Basic(optional = true) to Timesheet.companyContract:

 @Basic(optional = true) @ManyToOne @JoinColumn(name = "IDCONTRACT") private CompanyContract companyContract; 
+2
source share

This works great. I see that all related child entities are successfully deleted.

Check out the code below.

Use orphanRemoval = true, as shown below, instead of the deprecated org.hibernate.annotations.CascadeType.DELETE_ORPHAN method that you are using.

 @OneToMany(mappedBy = "companyContract", cascade = { CascadeType.ALL },orphanRemoval = true, fetch = FetchType.EAGER) 

Find the code below

 public void check() { System.out.println("Start check() in DummyDAOImpl"); Session session = sessionFactory.openSession(); CompanyContract companyContract = session.get(CompanyContract.class, 2); Profile p = companyContract.getProfile(); p.getCompanyContracts().remove(companyContract); companyContract.setProfile(null); session.update(p); session.flush(); session.close(); System.out.println("Executed check() in DummyDAOImpl"); } 
+1
source share

All Articles