frosty
frosty

Reputation: 5370

using lazy loading on property in nhibernate

In my product entity i have the following property. It works correctly and returns back the number of stock available. Looking at my profiler i can see that this statement is called everytime i call the product entity which is causing performance issues. Is there a way i can only call this property when needed. I've added 'lazy=true' but it is still called everytime. I'm using nhibernate 2. I'd prefer not to upgrade if poss.

 <property name="StockAvailable" lazy="true"  type="Int32" formula="(SELECT Stock - isnull((select Sum(oi.Quantity) from OrderItems oi inner join Orders o on oi.OrderId = o.Id where (oi.OrderItemStatusId = 0 or oi.OrderItemStatusId = 1)  and o.OrderStatus = 3 and oi.ProductId = Id), 0))"/>

Upvotes: 1

Views: 739

Answers (2)

Oliver Hanappi
Oliver Hanappi

Reputation: 12346

Lazy properties are only supported from NHibernate 3 and up, so you need to upgrade if you want that property to be on your entity.

Another possiblity is to create an entity which is lazy-loaded and has just this single property on it (you could even map it to the same table).

I don't know if this is an issue to you, but you should also consider creating a view for that calculation which uses group by. This is important if you want to load not a single but many entities at the same time as group by usually performs mich better than a subselect.

Upvotes: 2

jishi
jishi

Reputation: 24634

http://ayende.com/Blog/archive/2010/01/27/nhibernate-new-feature-lazy-properties.aspx

Looking at the date I don't think that made it into 2.1.2, since that was released in nov 2009.

Upvotes: 0

Related Questions