As you can see I am using soft/logical deletion on my system:
My entities:
@Where(clause = "deleted='false'")
public class Person {
//...
}
My services:
@Override
public ServiceResponse DeletePerson (Person entity) {
ServiceResponse sr = new ServiceResponse<>();
try {
sr = ValidateDeletePerson(entity); //Business logic treatment
if (sr.hasError())
return sr;
Person dbEntity = GetPerson(entity.getPersonID());
dbEntity.setDeleted(true);
_repository.save(dbEntity);
} catch (Exception ex) {
sr.getErrors().add(new ServiceError(ex));
}
return sr;
}
When the user try to delete an object - actually only the boolean deleted is switched to true on the DB as I demonstrate above -, I want to check if the object is being referenced to another before doing this logical deletion.
So, if the object is being used/referenced by other, I want to catch this action as an error, or another similar treatment to prevent the deletion.
Example, If a Person with ID 5
is attached to a Student
, the user cannot delete this person.
What is the "best practice" to prevent it?
Important EDIT:
1) Person is referenced in N tables, not only on Student, so I am investigating a generic way to achieve it (Maybe you can tell hibernate to check it?)
2) The idea is to do it programatically, avoiding modifications on DB.