Skip to content
Snippets Groups Projects
Commit f49415d0 authored by tpylak's avatar tpylak
Browse files

LMS-648 bidirectional connections between experiment and attachment.

SVN: 9032
parent 47569b41
No related branches found
No related tags found
No related merge requests found
...@@ -30,6 +30,7 @@ import javax.persistence.ManyToOne; ...@@ -30,6 +30,7 @@ import javax.persistence.ManyToOne;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator; import javax.persistence.SequenceGenerator;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint; import javax.persistence.UniqueConstraint;
import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.EqualsBuilder;
...@@ -113,19 +114,30 @@ public class AttachmentPE extends HibernateAbstractRegistrationHolder implements ...@@ -113,19 +114,30 @@ public class AttachmentPE extends HibernateAbstractRegistrationHolder implements
this.id = id; this.id = id;
} }
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @ManyToOne(fetch = FetchType.LAZY)
@NotNull(message = ValidationMessages.EXPERIMENT_NOT_NULL_MESSAGE) @NotNull(message = ValidationMessages.EXPERIMENT_NOT_NULL_MESSAGE)
@JoinColumn(name = ColumnNames.EXPERIMENT_COLUMN, updatable = false) @JoinColumn(name = ColumnNames.EXPERIMENT_COLUMN, updatable = false)
public ExperimentPE getParent() private ExperimentPE getParentInternal()
{ {
return parent; return parent;
} }
public void setParent(final ExperimentPE parent) void setParentInternal(final ExperimentPE parent)
{ {
this.parent = parent; this.parent = parent;
} }
@Transient
public ExperimentPE getParent()
{
return getParentInternal();
}
public void setParent(final ExperimentPE parent)
{
parent.addAttachment(this);
}
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@NotNull(message = ValidationMessages.ATTACHMENT_CONTENT_NOT_NULL_MESSAGE) @NotNull(message = ValidationMessages.ATTACHMENT_CONTENT_NOT_NULL_MESSAGE)
@JoinColumn(name = ColumnNames.EXPERIMENT_ATTACHMENT_CONTENT_COLUMN, updatable = false) @JoinColumn(name = ColumnNames.EXPERIMENT_ATTACHMENT_CONTENT_COLUMN, updatable = false)
......
...@@ -65,6 +65,7 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.ExperimentIdentifi ...@@ -65,6 +65,7 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.ExperimentIdentifi
import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.IdentifierHelper; import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.IdentifierHelper;
import ch.systemsx.cisd.openbis.generic.shared.dto.properties.EntityKind; import ch.systemsx.cisd.openbis.generic.shared.dto.properties.EntityKind;
import ch.systemsx.cisd.openbis.generic.shared.util.EqualsHashUtils; import ch.systemsx.cisd.openbis.generic.shared.util.EqualsHashUtils;
import ch.systemsx.cisd.openbis.generic.shared.util.HibernateUtils;
/** /**
* Persistence Entity representing experiment. * Persistence Entity representing experiment.
...@@ -281,15 +282,18 @@ public class ExperimentPE implements IEntityPropertiesHolder<ExperimentPropertyP ...@@ -281,15 +282,18 @@ public class ExperimentPE implements IEntityPropertiesHolder<ExperimentPropertyP
getExperimentProperties().add(property); getExperimentProperties().add(property);
} }
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "parent") @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "parentInternal")
@IndexedEmbedded @IndexedEmbedded
@Private @Private
// for Hibernate and bean conversion only
public Set<AttachmentPE> getExperimentAttachments() public Set<AttachmentPE> getExperimentAttachments()
{ {
return attachments; return attachments;
} }
@SuppressWarnings("unused")
@Private @Private
// for Hibernate and bean conversion only
public void setExperimentAttachments(final Set<AttachmentPE> attachments) public void setExperimentAttachments(final Set<AttachmentPE> attachments)
{ {
this.attachments = attachments; this.attachments = attachments;
...@@ -315,11 +319,29 @@ public class ExperimentPE implements IEntityPropertiesHolder<ExperimentPropertyP ...@@ -315,11 +319,29 @@ public class ExperimentPE implements IEntityPropertiesHolder<ExperimentPropertyP
// Package visibility to avoid bean conversion which will call an uninitialized field. // Package visibility to avoid bean conversion which will call an uninitialized field.
final void setAttachments(final Set<AttachmentPE> attachments) final void setAttachments(final Set<AttachmentPE> attachments)
{ {
for (final AttachmentPE experimentAttachment : attachments) getExperimentAttachments().clear();
for (final AttachmentPE attachment : attachments)
{
addAttachment(attachment);
}
}
public void addAttachment(final AttachmentPE child)
{
final ExperimentPE parent = child.getParent();
if (parent != null)
{
parent.getExperimentAttachments().remove(child);
}
child.setParentInternal(this);
// ensure that the collection is of the proper type - bean populator could set it to
// unmuttable empty list
if (getExperimentAttachments().size() == 0
&& getExperimentAttachments() instanceof HashSet == false)
{ {
experimentAttachment.setParent(this); setExperimentAttachments(new HashSet<AttachmentPE>());
} }
setExperimentAttachments(attachments); getExperimentAttachments().add(child);
} }
@Transient @Transient
...@@ -503,4 +525,9 @@ public class ExperimentPE implements IEntityPropertiesHolder<ExperimentPropertyP ...@@ -503,4 +525,9 @@ public class ExperimentPE implements IEntityPropertiesHolder<ExperimentPropertyP
{ {
return EntityKind.EXPERIMENT; return EntityKind.EXPERIMENT;
} }
public void ensureAttachmentsLoaded()
{
HibernateUtils.initialize(getExperimentAttachments());
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment