Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate: Conditionally load a calculated column

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?

like image 965
Alistair Avatar asked Mar 07 '26 00:03

Alistair


2 Answers

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.

like image 139
Diego Mijelshon Avatar answered Mar 08 '26 16:03

Diego Mijelshon


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 )

like image 22
DanP Avatar answered Mar 08 '26 15:03

DanP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!