79805865

Date: 2025-10-31 14:06:10
Score: 0.5
Natty:
Report link

I got it working with a native query like this:
Service:

@Transactional
public void deleteCommentWithChildren(Long parentId) {
List<Long> childIds = commentRepository.findChildIdsByParentId(parentId);
for (Long childId : childIds) {
deleteCommentWithChildren(childId);
}
commentRepository.deleteByIdQuery(parentId);
}

Repository:

@Query(value = "SELECT id FROM comment WHERE parent_comment_id = :parentId", nativeQuery = true)
List<Long> findChildIdsByParentId(@Param("parentId") Long parentId);

@Modifying
@Transactional
@Query(value = "DELETE FROM comment WHERE id = :id", nativeQuery = true)
void deleteByIdQuery(@Param("id") Long id);
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: LostQuotient117