Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Correct issue with History objects.
Browse files Browse the repository at this point in the history
Correct an issue that was causing a LazyLoadException in certain cases when trying to generate history objects.
  • Loading branch information
Carlos Munoz committed Apr 10, 2012
1 parent 4fc0805 commit 84dbb52
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
Expand Up @@ -116,7 +116,7 @@ public class HTextFlow extends HTextContainer implements Serializable, ITextFlow

private Map<HLocale, HTextFlowTarget> targets;

public Map<Integer, HTextFlowHistory> history;
private Map<Integer, HTextFlowHistory> history;

private HSimpleComment comment;

Expand All @@ -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()
{
Expand Down Expand Up @@ -279,6 +282,10 @@ public void setComment(HSimpleComment comment)
@Column(name = "content", nullable = false)
public List<String> 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<String>();
Expand All @@ -288,6 +295,10 @@ public List<String> getContents()

public void setContents(List<String> 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<String>(contents);
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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()
{
Expand Down
Expand Up @@ -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()
{
Expand Down Expand Up @@ -245,6 +248,10 @@ public void setContent( String content )
@Column(name = "content", nullable = false)
public List<String> 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<String>();
Expand All @@ -254,6 +261,10 @@ public List<String> getContents()

public void setContents(List<String> contents)
{
// Copy lazily loaded relations to the history object as this cannot be done
// in the entity callbacks
copyLazyLoadedRelationsToHistory();

this.contents = new ArrayList<String>(contents);
}

Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
Expand Up @@ -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
Expand All @@ -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()
{
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 84dbb52

Please sign in to comment.