79647689

Date: 2025-06-01 15:50:44
Score: 0.5
Natty:
Report link

I managed to find a solution. I replaced flashcardSetRepository.delete(flashcardSet) with

flashcardSet.getUser().getFlashcardSets().remove(flashcardSet);

Full code:

@Transactional
public void deleteFlashcardSet(Long userId, Long flashcardSetId) {

    FlashcardSet flashcardSet = flashcardSetRepository.findById(flashcardSetId)
            .orElseThrow(() -> new ResourceNotFoundException("Flashcard set not found"));

    if(!flashcardSet.getUser().getUserId().equals(userId)){
        throw new UnauthorizedException("User does not have permission to delete this flashcard set");
    }

    flashcardSet.getUser().getFlashcardSets().remove(flashcardSet);
}

Now I don't have to use entityManager.clear() or direct query.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Gruncio