From d89c1ede8bff1165d9486d496cb18319cbc7f8a9 Mon Sep 17 00:00:00 2001 From: Jonas Natten Date: Tue, 21 Jan 2025 09:28:17 +0100 Subject: [PATCH] Use `@Modifying(...)` to handle persistence context clearing/flushing This makes calling `wipeQualityEvaluationAverages` slightly safer i think :smile: --- .../ndla/taxonomy/repositories/NodeRepository.java | 2 +- .../taxonomy/service/QualityEvaluationService.java | 13 +------------ 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/src/main/java/no/ndla/taxonomy/repositories/NodeRepository.java b/src/main/java/no/ndla/taxonomy/repositories/NodeRepository.java index f8a66f50..8dedcd07 100644 --- a/src/main/java/no/ndla/taxonomy/repositories/NodeRepository.java +++ b/src/main/java/no/ndla/taxonomy/repositories/NodeRepository.java @@ -47,7 +47,7 @@ public interface NodeRepository extends TaxonomyRepository { """) Stream findNodesWithQualityEvaluation(); - @Modifying + @Modifying(flushAutomatically = true, clearAutomatically = true) @Query( """ UPDATE Node n diff --git a/src/main/java/no/ndla/taxonomy/service/QualityEvaluationService.java b/src/main/java/no/ndla/taxonomy/service/QualityEvaluationService.java index 866507f2..2b032c03 100644 --- a/src/main/java/no/ndla/taxonomy/service/QualityEvaluationService.java +++ b/src/main/java/no/ndla/taxonomy/service/QualityEvaluationService.java @@ -7,8 +7,6 @@ package no.ndla.taxonomy.service; -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; import java.net.URI; import java.util.Collection; import java.util.Optional; @@ -29,9 +27,6 @@ public class QualityEvaluationService { private final NodeRepository nodeRepository; - @PersistenceContext - private EntityManager entityManager; - public QualityEvaluationService(NodeRepository nodeRepository) { this.nodeRepository = nodeRepository; } @@ -127,15 +122,9 @@ public void updateEntireAverageTreeForNode(URI publicId) { nodeRepository.save(node); } - public void wipeQualityEvaluationAverages() { - nodeRepository.wipeQualityEvaluationAverages(); - nodeRepository.flush(); - entityManager.clear(); - } - @Transactional public void updateQualityEvaluationOfAllNodes() { - wipeQualityEvaluationAverages(); + nodeRepository.wipeQualityEvaluationAverages(); var nodeStream = nodeRepository.findNodesWithQualityEvaluation(); nodeStream.forEach( node -> updateQualityEvaluationOfParents(node, Optional.empty(), node.getQualityEvaluationGrade()));