Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.hisp.dhis.query.JpaQueryUtils.generateHqlQueryForSharingCheck;

import jakarta.persistence.EntityManager;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
Expand Down Expand Up @@ -919,7 +920,14 @@ public List<T> findByCreatedBy(@Nonnull UserDetails user) {
}

/**
* Look up objects which have property createdBy or lastUpdatedBy linked to given {@link User}
* Look up objects which have property createdBy or lastUpdatedBy linked to given {@link User}.
*
* <p>Runs with {@link FlushModeType#COMMIT}, so the JPA auto-flush (a full-session dirty-check)
* is skipped before this query. This is safe for the current caller (the read-only deletion veto
* phase, which runs before any deletion handler writes), and avoids a per-query flush that
* dominates the cost when a large object graph is in the session. If a future caller reads within
* a transaction and must observe earlier un-flushed changes, change this method to accept a
* flush-mode parameter rather than relaxing the mode for everyone.
*
* @param user the {@link User} for filtering
* @return TRUE of objects found. FALSE otherwise.
Expand All @@ -939,7 +947,12 @@ public boolean existsByUser(@Nonnull User user, final Set<String> checkPropertie
return false;
}
query.where(builder.or(predicates.toArray(new Predicate[0])));
return !entityManager.createQuery(query).setMaxResults(1).getResultList().isEmpty();
return !entityManager
.createQuery(query)
.setFlushMode(FlushModeType.COMMIT)
.setMaxResults(1)
.getResultList()
.isEmpty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ void testExistsByUser() {
DataElement dataElementA = createDataElement('A');
dataElementA.setCreatedBy(userA);
dataElementStore.save(dataElementA);
entityManager.flush();
assertTrue(dataElementStore.existsByUser(userA, Set.of("createdBy")));
assertFalse(dataElementStore.existsByUser(userB, Set.of("createdBy")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ void testDeleteCreatedByUser() {
DataElement dataElement = createDataElement('A');
dataElement.setCreatedBy(userA);
idObjectManager.save(dataElement);
entityManager.flush();

assertThrows(DeleteNotAllowedException.class, () -> userService.deleteUser(userA));
}
Expand All @@ -208,6 +209,7 @@ void testDeleteLastUpdatedByUser() {

dataElement.setDescription("Updated");
idObjectManager.update(dataElement);
entityManager.flush();

assertThrows(DeleteNotAllowedException.class, () -> userService.deleteUser(userA));
}
Expand Down
Loading