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 87e1d9b63f8..9af5a3c38b8 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 @@ -1322,29 +1322,31 @@ public class SearchBuilder { private void doInitializeSearch() { if (mySearchEntity == null) { + reinitializeSearch(); + } + } - mySearchEntity = new Search(); - mySearchEntity.setUuid(UUID.randomUUID().toString()); - mySearchEntity.setCreated(new Date()); - mySearchEntity.setTotalCount(-1); - mySearchEntity.setPreferredPageSize(myParams.getCount()); - mySearchEntity.setSearchType(myParams.getEverythingMode() != null ? SearchTypeEnum.EVERYTHING : SearchTypeEnum.SEARCH); - mySearchEntity.setLastUpdated(myParams.getLastUpdated()); + private void reinitializeSearch() { + mySearchEntity = new Search(); + mySearchEntity.setUuid(UUID.randomUUID().toString()); + mySearchEntity.setCreated(new Date()); + mySearchEntity.setTotalCount(-1); + mySearchEntity.setPreferredPageSize(myParams.getCount()); + mySearchEntity.setSearchType(myParams.getEverythingMode() != null ? SearchTypeEnum.EVERYTHING : SearchTypeEnum.SEARCH); + mySearchEntity.setLastUpdated(myParams.getLastUpdated()); - for (Include next : myParams.getIncludes()) { - mySearchEntity.getIncludes().add(new SearchInclude(mySearchEntity, next.getValue(), false, next.isRecurse())); + for (Include next : myParams.getIncludes()) { + mySearchEntity.getIncludes().add(new SearchInclude(mySearchEntity, next.getValue(), false, next.isRecurse())); + } + for (Include next : myParams.getRevIncludes()) { + mySearchEntity.getIncludes().add(new SearchInclude(mySearchEntity, next.getValue(), true, next.isRecurse())); + } + + if (myParams.isPersistResults()) { + myEntityManager.persist(mySearchEntity); + for (SearchInclude next : mySearchEntity.getIncludes()) { + myEntityManager.persist(next); } - for (Include next : myParams.getRevIncludes()) { - mySearchEntity.getIncludes().add(new SearchInclude(mySearchEntity, next.getValue(), true, next.isRecurse())); - } - - if (myParams.isPersistResults()) { - myEntityManager.persist(mySearchEntity); - for (SearchInclude next : mySearchEntity.getIncludes()) { - myEntityManager.persist(next); - } - } - } } @@ -1362,8 +1364,9 @@ public class SearchBuilder { private void doSetPids(Collection thePids) { if (myParams.isPersistResults()) { - mySearchResultDao.deleteForSearch(mySearchEntity.getId()); - mySearchResultDao.flush(); + if (mySearchEntity.getTotalCount() != null) { + reinitializeSearch(); + } LinkedHashSet results = new LinkedHashSet(); int index = 0; diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/Search.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/Search.java index 652d340e12d..eea9b1c5be7 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/Search.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/Search.java @@ -95,8 +95,8 @@ public class Search implements Serializable { @Column(name="SEARCH_TYPE", nullable=false) private SearchTypeEnum mySearchType; - @Column(name="TOTAL_COUNT") - private int myTotalCount; + @Column(name="TOTAL_COUNT", nullable=false) + private Integer myTotalCount; @Column(name="SEARCH_UUID", length=40, nullable=false, updatable=false) private String myUuid; @@ -145,7 +145,7 @@ public class Search implements Serializable { } - public int getTotalCount() { + public Integer getTotalCount() { return myTotalCount; } @@ -188,7 +188,7 @@ public class Search implements Serializable { mySearchType = theSearchType; } - public void setTotalCount(int theTotalCount) { + public void setTotalCount(Integer theTotalCount) { myTotalCount = theTotalCount; }