Source:
public List<ResourceHistoryTable> fetchEntities(RequestPartitionId thePartitionId, int theFromIndex, int theToIndex) { CriteriaBuilder cb = myEntityManager.getCriteriaBuilder(); CriteriaQuery<ResourceHistoryTable> criteriaQuery = cb.createQuery(ResourceHistoryTable.class); Root<ResourceHistoryTable> from = criteriaQuery.from(ResourceHistoryTable.class); addPredicatesToQuery(cb, thePartitionId, criteriaQuery, from); from.fetch("myProvenance", JoinType.LEFT); criteriaQuery.orderBy(cb.desc(from.get("myUpdated"))); TypedQuery<ResourceHistoryTable> query = myEntityManager.createQuery(criteriaQuery); query.setFirstResult(theFromIndex); query.setMaxResults(theToIndex - theFromIndex); List<ResourceHistoryTable> tables = query.getResultList(); if (tables.size() > 0) { ImmutableListMultimap<Long, ResourceHistoryTable> resourceIdToHistoryEntries = Multimaps.index(tables, ResourceHistoryTable::getResourceId); Map<Long, Optional<String>> pidToForcedId = myIdHelperService.translatePidsToForcedIds(resourceIdToHistoryEntries.keySet()); for (Long nextResourceId : resourceIdToHistoryEntries.keySet()) { List<ResourceHistoryTable> historyTables = resourceIdToHistoryEntries.get(nextResourceId); Optional<String> forcedId = myIdHelperService.translatePidIdToForcedId(new ResourcePersistentId(nextResourceId)); String resourceId; if (forcedId.isPresent()) { resourceId = historyTables.get(0).getResourceType() + '/' + forcedId.get(); } else { resourceId = historyTables.get(0).getResourceType() + '/' + nextResourceId; } for (ResourceHistoryTable nextHistoryTable : historyTables) { nextHistoryTable.setTransientForcedId(resourceId); } } } return tables; }

Target:
public List<ResourceHistoryTable> fetchEntities(RequestPartitionId thePartitionId, int theFromIndex, int theToIndex) { CriteriaBuilder cb = myEntityManager.getCriteriaBuilder(); CriteriaQuery<ResourceHistoryTable> criteriaQuery = cb.createQuery(ResourceHistoryTable.class); Root<ResourceHistoryTable> from = criteriaQuery.from(ResourceHistoryTable.class); addPredicatesToQuery(cb, thePartitionId, criteriaQuery, from); from.fetch("myProvenance", JoinType.LEFT); criteriaQuery.orderBy(cb.desc(from.get("myUpdated"))); TypedQuery<ResourceHistoryTable> query = myEntityManager.createQuery(criteriaQuery); query.setFirstResult(theFromIndex); query.setMaxResults(theToIndex - theFromIndex); List<ResourceHistoryTable> tables = query.getResultList(); if (tables.size() > 0) { ImmutableListMultimap<Long, ResourceHistoryTable> resourceIdToHistoryEntries = Multimaps.index(tables, ResourceHistoryTable::getResourceId); Map<Long, Optional<String>> pidToForcedId = myIdHelperService.translatePidsToForcedIds(resourceIdToHistoryEntries.keySet()); ourLog.trace("Translated IDs: {}", pidToForcedId); for (Long nextResourceId : resourceIdToHistoryEntries.keySet()) { List<ResourceHistoryTable> historyTables = resourceIdToHistoryEntries.get(nextResourceId); String resourceId; Optional<String> forcedId = pidToForcedId.get(nextResourceId); if (forcedId.isPresent()) { resourceId = forcedId.get(); } else { resourceId = nextResourceId.toString(); } for (ResourceHistoryTable nextHistoryTable : historyTables) { nextHistoryTable.setTransientForcedId(resourceId); } } } return tables; }