In an ORM, does “lazy loading” mean you may not ge

2019-08-10 02:36发布

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?

4条回答
Evening l夕情丶
2楼-- · 2019-08-10 02:52

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?

查看更多
淡お忘
3楼-- · 2019-08-10 02:54

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.

查看更多
\"骚年 ilove
4楼-- · 2019-08-10 03:01

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
}
查看更多
贼婆χ
5楼-- · 2019-08-10 03:04

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.

查看更多
登录 后发表回答