Prevent lazy loading in nHibernate

Posted by Ciaran on Stack Overflow See other posts from Stack Overflow or by Ciaran
Published on 2010-05-12T05:32:59Z Indexed on 2010/05/12 5:34 UTC
Read the original article Hit count: 327

Hi,

I'm storing some blobs in my database, so I have a Document table and a DocumentContent table. Document contains a filename, description etc and has a DocumentContent property.

I have a Silverlight client, so I don't want to load up and send the DocumentContent to the client unless I explicity ask for it, but I'm having trouble doing this.

I've read the blog post by Davy Brion. I have tried placing lazy=false in my config and removing the virtual access modifier but have had no luck with it as yet.

Every time I do a Session.Get(id), the DocumentContent is retrieved via an outer join. I only want this property to be populated when I explicity join onto this table and ask for it.

Any help is appreciated.

My NHibernate mapping is as follows:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Jrm.Model"
                   namespace="Jrm.Model">
  <class name="JrmDocument" lazy="false">
    <id name="JrmDocumentID">
      <generator class="native" />
    </id>

    <property name="FileName"/>
    <property name="Description"/>
    <many-to-one name="DocumentContent" class="JrmDocumentContent" unique="true" column="JrmDocumentContentID" lazy="false"/>
  </class>


  <class name="JrmDocumentContent" lazy="false">
    <id name="JrmDocumentContentID">
      <generator class="native" />
    </id>

    <property name="Content" type="BinaryBlob" lazy="false">
      <column name="FileBytes" sql-type="varbinary(max)"/>
    </property>

  </class>
</hibernate-mapping>

and my classes are:

[DataContract]   
    public class JrmDocument : ModelBase
    {
        private int jrmDocumentID;
        private JrmDocumentContent documentContent;
        private long maxFileSize;
        private string fileName;
        private string description;

        public JrmDocument()
        {
        }

        public JrmDocument(string fileName, long maxFileSize)
        {
            DocumentContent = new JrmDocumentContent(File.ReadAllBytes(fileName));
            FileName = new FileInfo(fileName).Name;
        }

        [DataMember]
        public virtual int JrmDocumentID
        {
            get { return jrmDocumentID; }
            set
            {
                jrmDocumentID = value;
                OnPropertyChanged("JrmDocumentID");
            }
        }               

        [DataMember]
        public JrmDocumentContent DocumentContent
        {
            get { return documentContent; }
            set
            {
                documentContent = value;
                OnPropertyChanged("DocumentContent");
            }
        }        

        [DataMember]
        public virtual long MaxFileSize
        {
            get { return maxFileSize; }
            set
            {
                maxFileSize = value;
                OnPropertyChanged("MaxFileSize");
            }
        }


        [DataMember]
        public virtual string FileName
        {
            get { return fileName; }
            set
            {
                fileName = value;
                OnPropertyChanged("FileName");
            }
        }

        [DataMember]
        public virtual string Description
        {
            get { return description; }
            set
            {
                description = value;
                OnPropertyChanged("Description");
            }
        }
    }



    [DataContract]
    public class JrmDocumentContent : ModelBase
    {
        private int jrmDocumentContentID;
        private byte[] content;

        public JrmDocumentContent()
        {
        }
        public JrmDocumentContent(byte[] bytes)
        {
            Content = bytes;
        }

        [DataMember]
        public int JrmDocumentContentID
        {
            get { return jrmDocumentContentID; }
            set
            {
                jrmDocumentContentID = value;
                OnPropertyChanged("JrmDocumentContentID");
            }
        }

        [DataMember]
        public byte[] Content
        {
            get { return content; }
            set
            {
                content = value;
                OnPropertyChanged("Content");
            }
        }
    }

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about nhibernate-mapping