开发者

NHibernate LazyLoad Individual Property When Accessed

I have a entity map where I have three properties marked for Lazy-Loading. My expectation would be that when I access an individual property (i.e., Thumbnail) then only that property's data is loaded. However, it appears that NHibernate will load all properties marked as being LazyLoaded if any one of those properties is accessed (i.e., accessing Thumbnail also loads HighRes and LowRes data).

Is there a way to change this behavior?

public sealed class LeakImageMap : ClassMap<LeakImageEntity>
{
  public LeakImageMap()
  {
    LazyLoad();
    Table("LeakImage");
    Id(x => x.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty);

    Map(x => x.FileName).Not.Nullable();
    Map(x => x.FileSize).Not.Nullable();
    Map(x => x.LeakId).Nullable();

    Map(x => x.Thumbnail).Not.Nullable().LazyLoad();
    Map(x => x.HighRes).Not.Nullable().LazyLoad();
    Map(x => x.LowRes).Not.Nullable().LazyLoad();
  }
}

Additional Info

Accessing image.Thumbnail generates the following SQL:

SELECT
    leakimagee_.Thumbnail as Thumbnail14_,
    leakimagee_.HighRes as HighRes14_,
    leakimagee_.LowRes as LowRes14_ 
FROM
    Hvcs.LeakImage leakimagee_ 
WHERE
    leakimagee_.Id=@p0;
@p0 = 7588d167-22b5-4f2e-b640-9ecb00ed9138 [Type: Guid (0)]

However, just want to have the following:

SELECT
    leakimagee_.Thumbnail as Thumbnail14_
FROM
    Hvcs.LeakImage leakimagee_ 
WHERE
    leakimagee_开发者_StackOverflow.Id=@p0;
@p0 = 7588d167-22b5-4f2e-b640-9ecb00ed9138 [Type: Guid (0)]


What about multiple lazy properties? NHibernate support them, but you need to keep one thing in mind. NHibernate will load all the entity’s lazy properties, not just the one that was immediately accessed.

from Lazy Properties


I guess this happens because in retrieving one of the lazyload properties hibernate finds that it can also get the others as a side product (columns in the table with no difference than the one of interest) of the SQL statement it generates and so it decides well why not populate these remaining properties with all this data that come in handy. (as I noticed in this case the relevant fields are all mapped as 'Map' so the above speculation might apply, though still not sure whether i got it right or not)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜