diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java index 6fc9a7f7b87..c15c9ab2f0c 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java @@ -119,9 +119,10 @@ public abstract class BaseHapiFhirResourceDao extends B TagDefinition def = getTagOrNull(TagTypeEnum.TAG, theScheme, theTerm, theLabel); if (def != null) { BaseTag newEntity = entity.addTag(def); - - myEntityManager.persist(newEntity); - myEntityManager.merge(entity); + if (newEntity.getTagId() == null) { + myEntityManager.persist(newEntity); + myEntityManager.merge(entity); + } } ourLog.info("Processed addTag {}/{} on {} in {}ms", new Object[] { theScheme, theTerm, theId, w.getMillisAndRestart() }); @@ -450,7 +451,9 @@ public abstract class BaseHapiFhirResourceDao extends B TagDefinition def = getTagOrNull(nextDef.getTagType(), nextDef.getSystem(), nextDef.getCode(), nextDef.getDisplay()); if (def != null) { BaseTag newEntity = entity.addTag(def); - myEntityManager.persist(newEntity); + if (newEntity.getTagId() == null) { + myEntityManager.persist(newEntity); + } } } } 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 812085c51a1..055ed717096 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 @@ -23,16 +23,7 @@ package ca.uhn.fhir.jpa.entity; import java.util.Collection; import java.util.Date; -import javax.persistence.Column; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; -import javax.persistence.FetchType; -import javax.persistence.JoinColumn; -import javax.persistence.Lob; -import javax.persistence.MappedSuperclass; -import javax.persistence.OneToOne; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; +import javax.persistence.*; import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.model.primitive.IdDt; diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseTag.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseTag.java index 62c049fd775..8f15307cce3 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseTag.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseTag.java @@ -10,7 +10,7 @@ package ca.uhn.fhir.jpa.entity; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -32,13 +32,17 @@ public class BaseTag implements Serializable { private static final long serialVersionUID = 1L; - @ManyToOne(cascade= {}) - @JoinColumn(name="TAG_ID", nullable=false) + @ManyToOne(cascade = {}) + @JoinColumn(name = "TAG_ID", nullable = false) private TagDefinition myTag; - @Column(name="TAG_ID", insertable=false,updatable=false) + @Column(name = "TAG_ID", insertable = false, updatable = false) private Long myTagId; + public Long getTagId() { + return myTagId; + } + public TagDefinition getTag() { return myTag; } @@ -46,6 +50,5 @@ public class BaseTag implements Serializable { public void setTag(TagDefinition theTag) { myTag = theTag; } - - + } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTable.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTable.java index e8b765460cf..3953ba04854 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTable.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTable.java @@ -93,8 +93,13 @@ public class ResourceHistoryTable extends BaseHasResource implements Serializabl } @Override - public BaseTag addTag(TagDefinition theDef) { - ResourceHistoryTag historyTag = new ResourceHistoryTag(this, theDef); + public ResourceHistoryTag addTag(TagDefinition theTag) { + for (ResourceHistoryTag next : getTags()) { + if (next.getTag().equals(theTag)) { + return next; + } + } + ResourceHistoryTag historyTag = new ResourceHistoryTag(this, theTag); getTags().add(historyTag); return historyTag; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceTable.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceTable.java index 75824cdcb7d..cd2d318436a 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceTable.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceTable.java @@ -250,10 +250,13 @@ public class ResourceTable extends BaseHasResource implements Serializable { @Override public ResourceTag addTag(TagDefinition theTag) { - ResourceTag tag = new ResourceTag(this, theTag); - if (!getTags().contains(tag)) { - getTags().add(tag); + for (ResourceTag next : getTags()) { + if (next.getTag().equals(theTag)) { + return next; + } } + ResourceTag tag = new ResourceTag(this, theTag); + getTags().add(tag); return tag; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TagDefinition.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TagDefinition.java index 13cd45265e0..0ef85dd8d40 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TagDefinition.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/TagDefinition.java @@ -10,7 +10,7 @@ package ca.uhn.fhir.jpa.entity; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -45,8 +45,8 @@ import ca.uhn.fhir.model.api.Tag; //@formatter:on @Entity -@Table(name = "HFJ_TAG_DEF", uniqueConstraints = { - @UniqueConstraint(name="IDX_TAGDEF_TYPESYSCODE", columnNames = { "TAG_TYPE", "TAG_SYSTEM", "TAG_CODE" }) +@Table(name = "HFJ_TAG_DEF", uniqueConstraints = { + @UniqueConstraint(name = "IDX_TAGDEF_TYPESYSCODE", columnNames = { "TAG_TYPE", "TAG_SYSTEM", "TAG_CODE" }) }) //@formatter:off public class TagDefinition implements Serializable { @@ -78,6 +78,8 @@ public class TagDefinition implements Serializable { @Enumerated(EnumType.ORDINAL) private TagTypeEnum myTagType; + private Integer myHashCode; + public TagDefinition() { } @@ -110,6 +112,7 @@ public class TagDefinition implements Serializable { public void setCode(String theCode) { myCode = theCode; + myHashCode = null; } public void setDisplay(String theDisplay) { @@ -118,10 +121,12 @@ public class TagDefinition implements Serializable { public void setSystem(String theSystem) { mySystem = theSystem; + myHashCode = null; } public void setTagType(TagTypeEnum theTagType) { myTagType = theTagType; + myHashCode = null; } public Tag toTag() { @@ -155,15 +160,14 @@ public class TagDefinition implements Serializable { @Override public int hashCode() { - HashCodeBuilder b = new HashCodeBuilder(); - if (myId != null) { - b.append(myId); - } else { + if (myHashCode == null) { + HashCodeBuilder b = new HashCodeBuilder(); b.append(myTagType); b.append(mySystem); b.append(myCode); + myHashCode = b.toHashCode(); } - return b.toHashCode(); + return myHashCode; } @Override diff --git a/src/changes/changes.xml b/src/changes/changes.xml index fe54a315f06..c27a14a119e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -100,7 +100,8 @@ Fix a regression in HAPI FHIR 2.5 JPA server where executing a search in a - transaction or batch operation caused an exception. + transaction or batch operation caused an exception. Thanks to Ravi Kuchi for + reporting! Correct an issue when processing transactions in JPA server where updates and