0

I have a problem. I have a domain that has child data. I can easily delete it by using Cascade delete but my teacher said that I must not cascade it. My plan is when the data has associations I will retrieve the associated and show it to the user. So far what I have is catching it by using try catch and I can display it on eclipse console. The display is:

java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (EPS.EMPLOYEE_EMPLOYER, CONSTRAINTFK_EMPLOYEE_EMPLOYER_EMPLOYEEFOREIGN KEY (EMPLOYEE_ID) REFERENCESEMPLOYEE(EMPLOYEE_ID))

Since it throws an ajax error I did an alert that says You cannot delete this record because it has associated record/s.. I want to be more specific.

Then I don't know what to do next. I want to retrieve all associated data and display it. Please help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
NinjaBoy
  • 3,715
  • 18
  • 54
  • 69

1 Answers1

3

I don't quite get your "solution". You are talking about deleting an entity with all its child entities. Retrieving the data and show to user has nothing to do with it.

First, I am not sure why your "teacher" tell you not to cascade. This is a perfect valid and appropriate situation to make use of cascading. Consider using delete-orphan and it will take the child entity delete for you.

If you really don't want to do cascading. What you need to do is to do the cascading manually: If you need to delete an entity, first get all its children, delete the children first, (may consider a flush here), and then delete the entity itself. Not much relationship with Hibernate in fact. That's a normal issue you will encounter when you works with DB.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131