views:

61

answers:

2

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?

+2  A: 

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.

Diego Mijelshon
A: 

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 )

DanP