NHibernate:有条件地加载计算列

发布于 2024-09-11 12:57:45 字数 321 浏览 9 评论 0原文

我有以下属性,

<property name="Allocated" type="decimal" formula="(select sum(a.AllocationAmount) from  Allocation a where  a.TransactionId = TransactionId)" />

它加载已分配给发票的交易金额,该交易运行良好。

然而,大多数情况下我并不关心这个金额。有没有办法有条件地加载这个计算列?或者有没有办法将此计算列添加到 HQL/Critera,以便我可以将其作为我运行的某些特定查询的一部分?

I have the following proprty

<property name="Allocated" type="decimal" formula="(select sum(a.AllocationAmount) from  Allocation a where  a.TransactionId = TransactionId)" />

This loads the amount of a Transaction that has been allocated to invoices which is working beautifully.

However, is most cases I don't care about this amount. Is there a way to conditionally load this calculated column? Or is there a way to add this calculated column to HQL/Critera so I can just make it part of some specific queries that I run?

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

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

发布评论

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

评论(2

不一样的天空 2024-09-18 12:57:45

在 NH 3.0 中,您可以定义惰性属性,其工作方式与听起来完全一样(请参阅 http://ayende.com/Blog/archive/2010/01/27/nhibernate-new-feature-lazy-properties.aspx

但是,这看起来更适合一个查询或者可能是一个充当视图的伪实体。

In NH 3.0 you can define lazy properties, which work exactly as they sound (see http://ayende.com/Blog/archive/2010/01/27/nhibernate-new-feature-lazy-properties.aspx)

However, this looks more suited for a query OR maybe a pseudo-entity that acts as a view.

最美不过初阳 2024-09-18 12:57:45

如果您在属性映射上指定 access="none",则可以将该属性全部保留在对象模型之外,但仍然可以使用 hql 对其进行查询。请参阅本文了解进一步的信息。

此外,您可以将该逻辑分解到一个函数中,使用自定义方言注册它,然后使用条件 api 对其进行查询/项目(通过 Projections.SqlFunction

If you specify access="none" on the property mapping, you can keep the property out of your object model all together, yet still query on it using hql. See this article for further information.

Also, you could factor that logic into a function, register it with a custom dialect and then query/project on it using the criteria api (via Projections.SqlFunction )

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