These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Lazy Loading Formula Properties?
PostPosted: Fri Sep 15, 2006 11:45 am 
Newbie

Joined: Tue Sep 12, 2006 8:48 am
Posts: 2
[NHibernate 1.0.2]

Hello,
In my entities, I have some properties which are actually formulas, such as counting all orders for a Person entity. However, I want these calculated properties to be calculated only on "special request", not every time I read the person entity from the database.

What is the best approach to do this? As far as I can see, there is no "lazy=true" attribute for properties.

Thanks for any help,
Urs


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 15, 2006 12:25 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Your property can just calculate the value and store it in a private variable (if you want to "cache" the result)

Lazy is specified for your persistent classes and collections, not calculated values.

If you want to do a calculation lazily, then you put the code in the property accessor and it won't be calc'ed until the calling code uses your property.

.. so in your case for Orders you might want the Orders property of a person to be lazy (which makes sense). Then your property would look like:

Code:
public int OrderCount
{
  get
  {
      Orders[] orders = this.Orders; //THIS MAKES THE DB CALL!
      return orders.length;
  }
}

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 15, 2006 12:36 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
even better, though... would be to create a custom HQL query something like "COUNT (*) FROM Orders o WHERE o.Person = p"
(my HQL might be off a tad)

But this way you avoid querying for ALL orders just to get the count.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.