NHibernate:有条件地加载计算列
我有以下属性,
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 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.
如果您在属性映射上指定
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
)