2

This is not a technical question but rather about the right logic on handling delete with foreign key constraint. In my exercise app, I cascaded the record with OneToMany relationship with other records. In other words when I delete the record, Hibernate will automatically delete the associated records.

My problem is, my teacher told me that cascading is not right. He said that the user must delete first the associated records and he didn't tell me the details and he is kinda angry when he told us. (I don't know what's his problem but I don't really mind. I just want to learn).

Please give me some advice because I cannot make up my mind. So far these are my thoughts:

1) After clicking delete, the system will tell the user that there are associated records and it will be deleted along if he continues. He will be then ask if he will continue.

2) After clicking delete, the system will tell the user that there are associated records and he can be redirected to a page or there is a pop-up that will show all the associated records and he can manually delete.

3) Don't allow the user to delete.

2 Answers 2

2

You approach the problem from the wrong direction - you ask for a general answer where there is none. The answer depends on a lot of details of the use case. Ask yourself:

  • what kind of data is managed by your exercise app?

  • does the user need the information about the associated records to make a decision about the deletion? Or can the system safely delete the associated records, and the additional information is of no value for the user?

  • what is the expected behaviour in case your user deletes the record accidentally? Can the data easily be restored?

  • shall any user have the right to delete those records and the associated records as well? Or can this cause further problems?

  • (... add more points like this by yourself)

Anyhow, it is good practice to ask such kind of questions before starting to code the deletion logic - if you did not ask before any of those questions, though your teacher gave you a chance to ask, he may rightfully question your solution.

0

The answer depends entirely on the system requirements.

Some systems do a "cascading delete," which deletes the related records before the primary record is deleted. Others set a flag on all deleted records, and only removes them from the database permanently when a certain amount of time has expired; this allows for undeletes to take place, if desired.

In all cases, it can be useful to notify the user when related records are about to deleted, or display the records to the user so that they can approve deletion. Security systems can prevent a user from deleting the records, if they don't have the right to do so. But ultimately it depends on what the requirements are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.