Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent lazy loading in nHibernate
    text
    copied!<p>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.</p> <p>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.</p> <p>I've read the blog post by <a href="http://davybrion.com/blog/2009/03/must-everything-be-virtual-with-nhibernate/" rel="nofollow noreferrer">Davy Brion</a>. I have tried placing lazy=false in my config and removing the virtual access modifier but have had no luck with it as yet.</p> <p>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. </p> <p>Any help is appreciated.</p> <p>My NHibernate mapping is as follows:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Jrm.Model" namespace="Jrm.Model"&gt; &lt;class name="JrmDocument" lazy="false"&gt; &lt;id name="JrmDocumentID"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property name="FileName"/&gt; &lt;property name="Description"/&gt; &lt;many-to-one name="DocumentContent" class="JrmDocumentContent" unique="true" column="JrmDocumentContentID" lazy="false"/&gt; &lt;/class&gt; &lt;class name="JrmDocumentContent" lazy="false"&gt; &lt;id name="JrmDocumentContentID"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property name="Content" type="BinaryBlob" lazy="false"&gt; &lt;column name="FileBytes" sql-type="varbinary(max)"/&gt; &lt;/property&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>and my classes are:</p> <pre><code>[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"); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload