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);