views:

145

answers:

4

I was reading about a feature of an ORM called lazy loading, which, it said, means that larger columns are only loaded when the application actually needs them. How would an ORM decide what a "large column" is - would this be, for example, a blob column that can contain a lot of data compared to, say, an unsigned integer column? And when it says it lazy loads the column, does this mean when you run a query, you might not get results for some of the larger columns when you expect to?

A: 

Yes, a blob column sounds exactly like it. Big VARCHARs, and similar stuff might fall into it as well.

And what it means is that the smaller columns with be cached in memory, but reading the bigger columns will trigger a database read before the value is returned.

Or, at least, that's a likely interpretation of what you are saying. WHICH ORM is it, exactly?

Daniel
+2  A: 

In NHibernates case, lazy loading is preventing the loading of collection properties on the object (normally by joining another table, or sub selecting) until you access the collection property.

For example, you might want to load all the customers, but if the customers had an orders property (collection of orders), you can load all the customers without the orders, and then lazy load the orders of a specific customer when you want to see the orders.

This saves many database calls when you do not necessarily need all the data up front.

Pete Davis
+1  A: 

The data gets fetched when you access it, not when you create the particular object/collection in question.

so something like (pseudocode):

private void doNothing(int id)
{
    PersistentObject po = BetByID<PersistentObject>(id)
}

doesn't actually touch the db ever; whereas:

private void doSomething(int id)
{
    PersistenObject po = GetByID<PersistentObject>(id)
    Console.Write(po.id.ToString()) // <- object is fetched here
}
SnOrfus
A: 

Common approach is to mark larger properties with "lazy load". Using Xml mapping or attributes. E.g. For DataObjects.NET you should use [Field(LazyLoad = true)] property attribute.

Alexis Kochetov