0

We are not using Fluent Nhibernate. We have a table with a blob column which we would like to load conditionally. We already have specified it as 'lazy' and lazy loading is working fine. Is there a way to not load data for that column at all on a certain condition ? (hence for the property in respective class)

For example, here is the mapping

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

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Blah.blah.blah" assembly="Blah.blah">

  <class name="MyDocument" table="HEAVY_DOCS">
  <id name="Id" column="docnum" type="decimal"> </id>
  <property name="docname" column="docname" />
  <property name="details" column="details" />
  <property name="doc_data" column="document_data" lazy="true"  />

</class>
</hibernate-mapping>

and below is the code getting document

private IList<MyDocument> FetchDocuments(IList<string> docId, bool laodData)
{

if(!laodData)
{
    docs = (from doc in Session.Query<MyDocument>()
               where docId.Contains(doc.docnum)
               select doc).ToList();
}
else
{
 //if laodData is false dont load column document_data 

}
return docs ;
}
lame_coder
  • 3,085
  • 3
  • 19
  • 21
  • What is the condition? I don't really understand - if the data is lazy loading, then it won't load unless you need it. If you need it, why would you not load it? Doesn't make sense. – cbp Aug 01 '14 at 00:02
  • @cbp added an example to illustrate better. – lame_coder Aug 01 '14 at 00:14
  • Your code is still quite confusing, but if your doc_data property is lazy loaded, and assuming lazy loading is working, it shouldn't be loaded anyway until you access the doc_data property. – cbp Aug 01 '14 at 01:54
  • @cbp as I mentioned in my question "Is there a way to not load data for that column at all on a certain condition ?" .. not load the data for the column at all is what our requirement is. There could be a **design** answer for this answer but I am looking specifically for technical possibility of solving it. – lame_coder Aug 01 '14 at 04:32
  • @lame_coder: Just don't access it at "certain condition" and it will not load. – epitka Aug 01 '14 at 13:27

1 Answers1

0

The way, how to drive column selection by our code, is to use projections. Adjusted code could look like this:

if(!laodData)
{
    docs = (from doc in Session.Query<MyDocument>()
               where docId.Contains(doc.docnum)
               select doc // complete object
           ).ToList();
}
else
{
    docs = (from doc in Session.Query<MyDocument>()
               where docId.Contains(doc.docnum)
               select new MyDocument            // in NHibernate world
               {                                // that would be called
                 docname= doc.docname,          // projection
                 details = doc.details,
                 // no "doc_data" for LARGE document
                 Id = doc.Id, 
               }
             ).ToList();

}

With QueryOver the projections can profit from NHibernate built in powerful Result Transformers, which can fill even protected properties. Maybe see this custom transformer example for deep (relations including) transformations of QueryOver projections...

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • The limitation could be protected id column... but the concept should work... Enjoy NHibernate... awesome tool ;) PS: the QueryOver is better implemented in NHibenrate... so ... maybe try to at least check that ;) – Radim Köhler Aug 02 '14 at 06:57