RED-6467: Implemented undeletion of dictionary entries via the criteria builder.

Hopefully, this solves the db-driver issue with large numbers of entries.
This commit is contained in:
Viktor Seifert 2023-04-12 13:02:07 +02:00
parent b14d56195e
commit 5092b00f41

View File

@ -4,6 +4,8 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
@ -33,6 +35,8 @@ public class EntryPersistenceService {
private final FalseRecommendationEntryRepository falseRecommendationEntryRepository;
private final JDBCWriteUtils jdbcWriteUtils;
private final EntityManager entityManager;
@Transactional
public void deleteEntries(String typeId, List<String> values, long version, DictionaryEntryType dictionaryEntryType) {
@ -108,7 +112,10 @@ public class EntryPersistenceService {
switch (dictionaryEntryType) {
case ENTRY -> {
var undeletedEntries = entryRepository.undeleteEntries(typeId, entries, version);
// TODO Remove all but one implementation for undeletion, once it is clear which one works
//var undeletedEntries = entryRepository.undeleteEntries(typeId, entries, version);
List<String> undeletedEntries = undeleteEntries(typeId, entries, version);
undeletedEntries.forEach(entries::remove);
var entryEntities = entries.stream().map(e -> {
@ -154,6 +161,28 @@ public class EntryPersistenceService {
}
private List<String> undeleteEntries(String typeId, Set<String> entries, long version) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
var undeleteEntriesQuery = criteriaBuilder.createCriteriaUpdate(DictionaryEntryEntity.class);
var entryRoot = undeleteEntriesQuery.from(DictionaryEntryEntity.class);
var typeIdPred = criteriaBuilder.equal(entryRoot.get("typeId"), typeId);
var entriesPred = entryRoot.get("value").in(entries);
undeleteEntriesQuery.where(typeIdPred, entriesPred);
undeleteEntriesQuery.set("version", version);
undeleteEntriesQuery.set("deleted", false);
entityManager.createQuery(undeleteEntriesQuery).executeUpdate();
var existingEntriesQuery = criteriaBuilder.createQuery(DictionaryEntryEntity.class);
entryRoot = existingEntriesQuery.from(DictionaryEntryEntity.class);
undeleteEntriesQuery.where(typeIdPred, criteriaBuilder.equal(entryRoot.get("deleted"), false));
return entityManager.createQuery(existingEntriesQuery).getResultStream().map(DictionaryEntryEntity::getValue).toList();
}
public void cloneEntries(String originalTypeId, String newTypeId) {
entryRepository.cloneEntries(originalTypeId, newTypeId);