访问时的 NHibernate LazyLoad 单个属性

发布于 2024-11-03 00:36:20 字数 1267 浏览 3 评论 0原文

我有一个实体映射,其中三个属性标记为延迟加载。我的期望是,当我访问单个属性(即缩略图)时,仅加载该属性的数据。然而,如果访问其中任何一个属性,NHibernate 将加载所有标记为 LazyLoaded 的属性(即,访问缩略图也会加载 HighRes 和 LowRes 数据)。

有办法改变这种行为吗?

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();
  }
}

其他信息

访问 image.Thumbnail 会生成以下 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)]

但是,只想拥有以下内容:

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

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_.Id=@p0;
@p0 = 7588d167-22b5-4f2e-b640-9ecb00ed9138 [Type: Guid (0)]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2024-11-10 00:36:20

多个惰性属性怎么样? NHibernate 支持它们,但您需要记住一件事。 NHibernate 将加载实体的所有惰性属性,而不仅仅是立即访问的属性。

来自 惰性属性

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

剑心龙吟 2024-11-10 00:36:20

我想发生这种情况是因为在检索延迟加载属性之一时,hibernate 发现它还可以将其他属性作为它生成的 SQL 语句的副产品(表中的列与感兴趣的列没有区别),因此它决定得很好为什么不使用所有这些有用的数据来填充这些剩余的属性。 (正如我注意到在这种情况下相关字段都映射为“地图”,因此上述猜测可能适用,尽管仍然不确定我是否正确)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文