diff --git a/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java b/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java index 999c2d4c4..3bc1de1bb 100644 --- a/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java +++ b/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java @@ -239,18 +239,30 @@ private void evictPossiblyCachedReferences(Term term) { em.getEntityManagerFactory().getCache().evict(Term.class, term.getUri(), null); em.getEntityManagerFactory().getCache().evict(TermDto.class, term.getUri(), null); em.getEntityManagerFactory().getCache().evict(TermInfo.class, term.getUri(), null); + // Should be replaced by implementation of https://github.com/kbss-cvut/jopa/issues/92 Utils.emptyIfNull(term.getParentTerms()).forEach(pt -> { em.getEntityManagerFactory().getCache().evict(Term.class, pt.getUri(), null); em.getEntityManagerFactory().getCache().evict(TermDto.class, pt.getUri(), null); subTermsCache.evict(pt.getUri()); }); - term.setSubTerms(getSubTerms(term)); // Should be replaced by implementation of https://github.com/kbss-cvut/jopa/issues/92 - Utils.emptyIfNull(term.getSubTerms()) - .forEach(st -> { - em.getEntityManagerFactory().getCache().evict(Term.class, st.getUri(), null); - em.getEntityManagerFactory().getCache().evict(TermDto.class, st.getUri(), null); - }); + evictAllCachedDescendants(term); + } + + /** + * Evicts all descendants of the specified term from the cache - default context. + *

+ * This is done to prevent stale references through the parentTerms chain. + * + * @param term Term whose descendants to evict + */ + private void evictAllCachedDescendants(Term term) { + em.createNativeQuery("SELECT ?child WHERE { ?t ?hasChild* ?child . }", URI.class) + .setParameter("hasChild", URI.create(SKOS.NARROWER)) + .setParameter("t", term).getResultStream().forEach(st -> { + em.getEntityManagerFactory().getCache().evict(Term.class, st, null); + em.getEntityManagerFactory().getCache().evict(TermDto.class, st, null); + }); } /** diff --git a/src/main/java/cz/cvut/kbss/termit/service/business/TermService.java b/src/main/java/cz/cvut/kbss/termit/service/business/TermService.java index 1578b4888..e82868a21 100644 --- a/src/main/java/cz/cvut/kbss/termit/service/business/TermService.java +++ b/src/main/java/cz/cvut/kbss/termit/service/business/TermService.java @@ -446,8 +446,8 @@ public void remove(@Nonnull Term term) { @Transactional(propagation = Propagation.REQUIRES_NEW) @PreAuthorize("@termAuthorizationService.canModify(#term)") public void analyzeTermDefinition(AbstractTerm term, URI vocabularyIri) { - term = findRequired(term.getUri()); // required when throttling for persistent context Objects.requireNonNull(term); + term = repositoryService.findRequired(term.getUri()); // required when throttling for persistent context if (term.getDefinition() == null || term.getDefinition().isEmpty()) { return; }