diff --git a/server/zanata-model/src/main/java/org/zanata/model/HTextFlow.java b/server/zanata-model/src/main/java/org/zanata/model/HTextFlow.java index 0b10a7792e..18deae3812 100644 --- a/server/zanata-model/src/main/java/org/zanata/model/HTextFlow.java +++ b/server/zanata-model/src/main/java/org/zanata/model/HTextFlow.java @@ -116,7 +116,7 @@ public class HTextFlow extends HTextContainer implements Serializable, ITextFlow private Map targets; - public Map history; + private Map history; private HSimpleComment comment; @@ -133,6 +133,9 @@ public class HTextFlow extends HTextContainer implements Serializable, ITextFlow // Only for internal use (persistence transient) private HTextFlowHistory initialState; + + // Only for internal use (persistence transient) + private boolean lazyRelationsCopied = false; public HTextFlow() { @@ -279,6 +282,10 @@ public void setComment(HSimpleComment comment) @Column(name = "content", nullable = false) public List getContents() { + // Copy lazily loaded relations to the history object as this cannot be done + // in the entity callbacks + copyLazyLoadedRelationsToHistory(); + if( contents == null ) { contents = new ArrayList(); @@ -288,6 +295,10 @@ public List getContents() public void setContents(List contents) { + // Copy lazily loaded relations to the history object as this cannot be done + // in the entity callbacks + copyLazyLoadedRelationsToHistory(); + if (!equal(this.contents, contents)) { this.contents = new ArrayList(contents); @@ -421,6 +432,19 @@ private void updateInternalHistory() { this.oldRevision = this.revision; this.initialState = new HTextFlowHistory(this); + this.lazyRelationsCopied = false; + } + + /** + * Copies all lazy loaded relations to the history object. + */ + private void copyLazyLoadedRelationsToHistory() + { + if( this.initialState != null && this.initialState.getContents() == null && !this.lazyRelationsCopied ) + { + this.initialState.setContents( this.contents ); + this.lazyRelationsCopied = true; + } } /** diff --git a/server/zanata-model/src/main/java/org/zanata/model/HTextFlowHistory.java b/server/zanata-model/src/main/java/org/zanata/model/HTextFlowHistory.java index f7bb4e83cc..7ec2856566 100644 --- a/server/zanata-model/src/main/java/org/zanata/model/HTextFlowHistory.java +++ b/server/zanata-model/src/main/java/org/zanata/model/HTextFlowHistory.java @@ -65,7 +65,9 @@ public HTextFlowHistory(HTextFlow textFlow) { this.revision = textFlow.getRevision(); this.textFlow = textFlow; - this.setContents(textFlow.getContents()); + // This cannot be done at this point due to an issue with hibernate in which a listener cannot access + // loading collections + //this.setContents(textFlow.getContents()); } @Id @@ -95,7 +97,7 @@ public void setRevision(Integer revision) // TODO PERF @NaturalId(mutable=false) for better criteria caching @NaturalId - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "tf_id") public HTextFlow getTextFlow() { diff --git a/server/zanata-model/src/main/java/org/zanata/model/HTextFlowTarget.java b/server/zanata-model/src/main/java/org/zanata/model/HTextFlowTarget.java index 07e2288904..06d6052672 100644 --- a/server/zanata-model/src/main/java/org/zanata/model/HTextFlowTarget.java +++ b/server/zanata-model/src/main/java/org/zanata/model/HTextFlowTarget.java @@ -113,6 +113,9 @@ public class HTextFlowTarget extends ModelEntityBase implements HasContents, Has // Only for internal use (persistence transient) private HTextFlowTargetHistory initialState; + // Only for internal use (persistence transient) + private boolean lazyRelationsCopied = false; + public HTextFlowTarget() { @@ -245,6 +248,10 @@ public void setContent( String content ) @Column(name = "content", nullable = false) public List getContents() { + // Copy lazily loaded relations to the history object as this cannot be done + // in the entity callbacks + copyLazyLoadedRelationsToHistory(); + if( contents == null ) { contents = new ArrayList(); @@ -254,6 +261,10 @@ public List getContents() public void setContents(List contents) { + // Copy lazily loaded relations to the history object as this cannot be done + // in the entity callbacks + copyLazyLoadedRelationsToHistory(); + this.contents = new ArrayList(contents); } @@ -309,6 +320,19 @@ private void updateInternalHistory() { this.oldVersionNum = this.getVersionNum(); this.initialState = new HTextFlowTargetHistory(this); + this.lazyRelationsCopied = false; + } + + /** + * Copies all lazy loaded relations to the history object. + */ + private void copyLazyLoadedRelationsToHistory() + { + if( this.initialState != null && this.initialState.getContents() == null && !this.lazyRelationsCopied ) + { + this.initialState.setContents( this.contents ); + this.lazyRelationsCopied = true; + } } /** diff --git a/server/zanata-model/src/main/java/org/zanata/model/HTextFlowTargetHistory.java b/server/zanata-model/src/main/java/org/zanata/model/HTextFlowTargetHistory.java index 4536106413..b1c0aea383 100644 --- a/server/zanata-model/src/main/java/org/zanata/model/HTextFlowTargetHistory.java +++ b/server/zanata-model/src/main/java/org/zanata/model/HTextFlowTargetHistory.java @@ -100,7 +100,9 @@ public HTextFlowTargetHistory(HTextFlowTarget target) this.textFlowRevision = target.getTextFlowRevision(); this.textFlowTarget = target; this.versionNum = target.getVersionNum(); - this.setContents(target.getContents()); + // This cannot be done at this point due to an issue with hibernate in which a listener cannot access + // loading collections + //this.setContents(target.getContents()); } @Id @@ -117,7 +119,7 @@ protected void setId(Long id) // TODO PERF @NaturalId(mutable=false) for better criteria caching @NaturalId - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "target_id") public HTextFlowTarget getTextFlowTarget() { @@ -171,7 +173,7 @@ public void setLastChanged(Date lastChanged) this.lastChanged = lastChanged; } - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "last_modified_by_id", nullable = true) @Override public HPerson getLastModifiedBy()