如何一次性设置 nHibernate ICriteria 的所有关联上的 Fetchmode?

发布于 2024-08-16 02:10:40 字数 432 浏览 4 评论 0原文

我有一个与其他对象有很多关联的对象。所有这些都是由 nHibernate 延迟获取的,这在几乎所有情况下都很好。

在特定场景中,在本例中导出大量记录,我想将 Fetchmode 设置为渴望所有关联。有没有一种方法可以做到这一点,而不必手动指定每一个:

ICriteria crit = CreateCriteria().
  .SetFetchMode("Address", FetchMode.Eager)
  .SetFetchMode("ContactPerson", FetchMode.Eager);

我想找到的方法,但一直无法:

// This doesn't work.
ICriteria crit = CreateCriteria().SetFetchMode(FetchMode.Eager);

I have an object that has many assocations to other objects. All of these are fetched lazily by nHibernate, which is good in almost all cases.

In a particular scenario, in this case an export of a lot of records, I want to set the Fetchmode to eager on all associations. Is there a way to do this, without having to manually specify each one:

ICriteria crit = CreateCriteria().
  .SetFetchMode("Address", FetchMode.Eager)
  .SetFetchMode("ContactPerson", FetchMode.Eager);

The method I would like to find, but haven't been able to:

// This doesn't work.
ICriteria crit = CreateCriteria().SetFetchMode(FetchMode.Eager);

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

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

发布评论

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

评论(2

疏忽 2024-08-23 02:10:40

您可以尝试使用 NHibernate 元数据。

ISessionFactory sessionFactory;

Type type = typeof(MyEntity);
IClassMetadata meta = sessionFactory.GetClassMetadata(type);
foreach (string mappedPropertyName in meta.PropertyNames)
{
    IType propertyType = meta.GetPropertyType(mappedPropertyName);
    if (propertyType.IsAssociationType)
    {
      // initialize property
      // recursively go through the properties of the associated entity
    }

    if (propertyType.IsCollectionType)
    {
      // initialize collection
      // if it is a collection of entities, 
      // recursively go through the properties of the associated entity

      // Use NHibernateUtil.Initialize
    }
}

我不确定这是否值得付出努力。

You could try to use the NHibernate Metadata.

ISessionFactory sessionFactory;

Type type = typeof(MyEntity);
IClassMetadata meta = sessionFactory.GetClassMetadata(type);
foreach (string mappedPropertyName in meta.PropertyNames)
{
    IType propertyType = meta.GetPropertyType(mappedPropertyName);
    if (propertyType.IsAssociationType)
    {
      // initialize property
      // recursively go through the properties of the associated entity
    }

    if (propertyType.IsCollectionType)
    {
      // initialize collection
      // if it is a collection of entities, 
      // recursively go through the properties of the associated entity

      // Use NHibernateUtil.Initialize
    }
}

I'm not sure if it is worth the effort.

或十年 2024-08-23 02:10:40

不,没有办法以一揽子方式做到这一点。

No, there is no way to do this in a blanket fashion.

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