From 103b587ab5c3c2c90cadc5a3eae7ae28d697f5c0 Mon Sep 17 00:00:00 2001 From: Frank Tao Date: Thu, 21 Jun 2018 22:32:08 -0400 Subject: [PATCH] #1008: Fixed sql looping issue --- .../ca/uhn/fhir/jpa/dao/SearchBuilder.java | 29 +++++++++++++++++++ .../uhn/fhir/jpa/dao/data/IForcedIdDao.java | 5 +++- .../uhn/fhir/jpa/entity/BaseHasResource.java | 2 +- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java index f43ea0aef28..978826d5bf3 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java @@ -87,6 +87,7 @@ import ca.uhn.fhir.jpa.dao.data.IResourceIndexedSearchParamUriDao; import ca.uhn.fhir.jpa.dao.data.IResourceTagDao; import ca.uhn.fhir.jpa.entity.BaseHasResource; import ca.uhn.fhir.jpa.entity.BaseResourceIndexedSearchParam; +import ca.uhn.fhir.jpa.entity.ForcedId; import ca.uhn.fhir.jpa.entity.ResourceHistoryTable; import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamDate; import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamNumber; @@ -1688,8 +1689,19 @@ public class SearchBuilder implements ISearchBuilder { //-- preload all tags with tag definition if any Map> tagMap = getResourceTagMap(resultList); + //-- pre-load all forcedId + Map forcedIdMap = getForcedIdMap(pids); + + ForcedId forcedId = null; + Long resourceId = null; for (ResourceTable next : resultList) { Class resourceType = context.getResourceDefinition(next.getResourceType()).getImplementingClass(); + + resourceId = next.getId(); + forcedId = forcedIdMap.get(resourceId); + if (forcedId != null) + next.setForcedId(forcedId); + IBaseResource resource = theDao.toResource(resourceType, next, historyMap.get(next.getId()), tagMap.get(next.getId()), theForHistoryOperation); if (resource == null) { ourLog.warn("Unable to find resource {}/{}/_history/{} in database", next.getResourceType(), next.getIdDt().getIdPart(), next.getVersion()); @@ -1775,6 +1787,23 @@ public class SearchBuilder implements ISearchBuilder { return tagMap; } + //-- load all forcedId in to the map + private Map getForcedIdMap(Collection pids) { + + Map forceIdMap = new HashMap(); + + if (pids.size() == 0) + return forceIdMap; + + Collection forceIdList = myForcedIdDao.findByResourcePids(pids); + + for (ForcedId forcedId : forceIdList) { + + forceIdMap.put(forcedId.getResourcePid(), forcedId); + } + + return forceIdMap; + } @Override public void loadResourcesByPid(Collection theIncludePids, List theResourceListToPopulate, Set theRevIncludedPids, boolean theForHistoryOperation, EntityManager entityManager, FhirContext context, IDao theDao) { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/IForcedIdDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/IForcedIdDao.java index 2d24d6c7b00..9e2dc7c63e0 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/IForcedIdDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/IForcedIdDao.java @@ -1,5 +1,6 @@ package ca.uhn.fhir.jpa.dao.data; +import java.util.Collection; import java.util.List; /* @@ -38,5 +39,7 @@ public interface IForcedIdDao extends JpaRepository { @Query("SELECT f FROM ForcedId f WHERE f.myResourcePid = :resource_pid") public ForcedId findByResourcePid(@Param("resource_pid") Long theResourcePid); - + + @Query("SELECT f FROM ForcedId f WHERE f.myResourcePid in (:pids)") + Collection findByResourcePids(@Param("pids") Collection pids); } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseHasResource.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseHasResource.java index 42cbf10057f..177220e1b37 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseHasResource.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseHasResource.java @@ -42,7 +42,7 @@ public abstract class BaseHasResource { @OptimisticLock(excluded = true) private FhirVersionEnum myFhirVersion; - @OneToOne(optional = true, fetch = FetchType.EAGER, cascade = {}, orphanRemoval = false) + @OneToOne(optional = true, fetch = FetchType.LAZY, cascade = {}, orphanRemoval = false) @JoinColumn(name = "FORCED_ID_PID") @OptimisticLock(excluded = true) private ForcedId myForcedId;