I successfully implemented a soft delete (aka delete flag) for the entities of my applications. However, I have one remaining problem. I've written a custom JPARepository with findAll and count methods that filter out the deleted ones. I do this with the specification:
softDeleteSpecification = new Specification<T>() {
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.or(cb.isNull(root.get(DELETED_FIELD_NAME)), cb.equal(root.<T>get(DELETED_FIELD_NAME), false));
}
};
If the entity has for example a OneToMany child list of entities that are also soft deleted, this list is not filtered because the query is not run by its repository.
My question: Can I modify the specification above so that children that are soft deleted are filtered out? An alternative would be filtering the childs with reflection (manually after the query), but that wouldn't be performant.