Skip to content

Commit

Permalink
Use @Modifying(...) to handle persistence context clearing/flushing
Browse files Browse the repository at this point in the history
This makes calling `wipeQualityEvaluationAverages` slightly safer i
think 😄
  • Loading branch information
jnatten committed Jan 21, 2025
1 parent 540f2e4 commit d89c1ed
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public interface NodeRepository extends TaxonomyRepository<Node> {
""")
Stream<Node> findNodesWithQualityEvaluation();

@Modifying
@Modifying(flushAutomatically = true, clearAutomatically = true)
@Query(
"""
UPDATE Node n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,9 +27,6 @@ public class QualityEvaluationService {

private final NodeRepository nodeRepository;

@PersistenceContext
private EntityManager entityManager;

public QualityEvaluationService(NodeRepository nodeRepository) {
this.nodeRepository = nodeRepository;
}
Expand Down Expand Up @@ -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()));
Expand Down

0 comments on commit d89c1ed

Please sign in to comment.