Let's say I want to delete my entity A with a ManyToOne relation to entity B. After deleting entity A, if that was the last entity related to entity B, I also want to delete entity B within the same transaction. My implementation for these actions looks like the following:
@Transactional
fun delete(idEntityA: Long, idEntityB: Long) {
entityARepository.deleteById(idEntityA)
if (entityARepository.countDistinctByEntityBId(idEntityB) == 0L) {
entityBRepository.deleteById(idEntityB)
}
}
According to my understanding of how Spring Data JPA(/Hibernate) works, the count operation should not take into account the preceding delete operation, as it has not been committed yet. Although if I execute the method, it already takes into account the uncommitted delete operation of entity A. My question now, as I can't find proper documentation: Does Spring Data JPA or Hibernate also take into account uncommitted operations from the same transaction when reading data from the database?
I know that if I would execute the count query in a different transaction with the default isolation mode, it would not know about the delete operation if the respective transaction would not be committed yet.
Thanks in advance!