Good day.
Encountered the following problem.
Let's say you can leave request, together with the attachment.
When I want to view a list of all attachments, or modify the attachment (its name, type, or even binary body) makes no sense to load the binary itself a body.
I'm using NHibernate version 2.1.0.4 does not support lazy property.
Implemented lazy loading of an attachment as follows.
One attachment stored in table in two lines, ie for example in the first line and kept all information except for the binary of the body, as well as a link to the second line (AttachmentBodyID), and in the second row of stored body itself a binary, other fields (except ID, IsDeleted, ROW_V) are filled with NULL.
Attachments table contains the following columns:
[ID]
,[TypeAttachID]
,[Name]
,[ChangedDate]
,[ChangedByID]
,[Doc] - binary body
,[IsDeleted]
,[ROW_V]
,[AttachmentBodyID] – link to binary body
Created two entities. Attachment and AttachmentBody.
Here they are
Code:
[KnownType(typeof(Attachment))]
[DataContract(IsReference = true)]
[Entity(NameMapping.Attachment)]
public partial class Attachment : BaseNamedEntity
{
[DataMember]
public virtual TypeAttach TypeAttach { get; set; }
[DataMember]
public virtual DateTime ChangedDate { get; set; }
[DataMember]
public virtual User ChangedEmployee { get; set; }
[DataMember]
public virtual AttachmentBody AttachmentBody { get; set; }
}
[KnownType(typeof(AttachmentBody))]
[DataContract(IsReference = true)]
[Entity(NameMapping.AttachmentBody)]
public class AttachmentBody : BaseNamedEntity
{
[DataMember]
public virtual byte[] Content { get; set; }
}
}
Mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="SMRR.CCWP.Entities" namespace="SMRR.CCWP.Entities.Business">
<class name="Attachment" table="Attachments" where="AttachmentBodyID IS NOT NULL">
<id name="Id" type="Int64" column="ID" unsaved-value="0">
<generator class="identity"/>
</id>
<version name="Version" column="ROW_V"/>
<property column="IsDeleted" type="Boolean" name="IsDeleted"/>
<property column="Name" type="String" name="Name"/>
<many-to-one name="AttachmentBody" class="SMRR.CCWP.Entities.Business.AttachmentBody, SMRR.CCWP.Entities" column="AttachmentBodyID" lazy="proxy" cascade="all-delete-orphan"/>
<property column="ChangedDate" type="DateTime" name="ChangedDate"/>
<many-to-one name="TypeAttach" lazy="false" column="TypeAttachID" cascade="none"
class="SMRR.CCWP.Entities.Directories.TypeAttach, SMRR.CCWP.Entities"/>
<many-to-one name="ChangedEmployee" lazy="false" column="ChangedByID" cascade="none"
class="SMRR.CCWP.Entities.Administration.User, SMRR.CCWP.Entities"/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="SMRR.CCWP.Entities" namespace="SMRR.CCWP.Entities.Business">
<class name="AttachmentBody" table="Attachments" where="AttachmentBodyID IS NULL">
<id name="Id" type="Int64" column="ID" unsaved-value="0">
<generator class="identity"/>
</id>
<version name="Version" column="ROW_V"/>
<property column="IsDeleted" type="Boolean" name="IsDeleted"/>
<property name="Content" type="BinaryBlob" column="Doc"/>
</class>
</hibernate-mapping>
Then, when I selected the attachment it boots up without binary body,
in the end,
I have an object "attachment" in which "attachmnentbody = null". Changing the name of the attachment I sent him to save, I saving use "Merge". (Otherwise, not work deleting attachments).
As a result, in the database is reset AttachmnentBodyID to null !
I lost a link to a binary body!
I do not want this behavior,
I need, that when saving an object, reference to the binary body has not been lost
Of course possible load full attachment(with binary body) but it's a bad decision.
Please tell me how to implement it?