-->
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.  [ 5 posts ] 
Author Message
 Post subject: how to implement computed property
PostPosted: Thu May 26, 2005 5:13 pm 
Newbie

Joined: Thu May 26, 2005 4:50 pm
Posts: 7
I just recently started with NHibernate and have been able to find most of my answers in the forums, documention, and google.

However, I have discovered an issue that someone here might be able to help me with.

Say I have two tables in a database, Device and CalibrationEvent. The calibration event has an associated Device and a time of calibration (CalibrationDate) property.

In my Device mapping file I have CalibrationEvent as a mapped set called PastCalibrations set to lazy load. Now, there can be very large number of CalibrationEvents for a Device. I'm trying to figure out a way to create a property for the Device object that returns the most recent calibration date without iterating through the entire set PastCalibrations.

In my application, I'd like to say something like "myDevice.MostRecentCalibration.CalibrationDate" and get the information I'm looking for. Should I be tyring to do this another way?

I was thinking of having the mapping order the set decending by CalibrationDate and then have the MostRecentCalibration property return the first element in the set. Will this cause the entire PastCalibrations set to be lazy-loaded from the database or just the first element?

I would appreciate any tips or suggestions....even "RTFM" as I'd be happy to know it is in there and I just need to read it more closely.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 5:41 pm 
Regular
Regular

Joined: Thu May 12, 2005 10:12 am
Posts: 71
Location: Buenos Aires, Argentina
I believe this is what Filter are for. I haven't tryied them yet since my current project has not gone that far (yet), but basically you do something like this

Code:
IList results = session.CreateFilter(myDevice.MostRecentCalibration, "from CalibrationEvent c where c.CalibrationDate > :date")


I'm not sure this code is even right so let us know your results.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 9:41 pm 
Regular
Regular

Joined: Mon May 16, 2005 1:35 am
Posts: 67
I haven't been using NHibernate long so I could be completely off base here, but you could try looking at using the "where" attribute on the "set" element. You could map the set using the "field" access strategy and keep the field private. And then in your MostRecentCalibration getter, get the first element of the private Set field.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 10:12 am 
Newbie

Joined: Thu May 26, 2005 4:50 pm
Posts: 7
roniburd,

I think filters would work great, but which layer do they belong in? If I put it in my Device class, doesn't that tie the domain layer to the data layer? I've never found an example of anyone putting nhibernate stuff in with their domain classes.

I guess if I explain my model a little better.....
I have web, data, service, and domain layers. The web layer calls static methods of the service classes (like DeviceService.GetAllDevices()), which in turn use a session (created at beginning of request and flushed at end) managed by the data layer and return collections of domain objects from the domain layer.

Can you recommend an example project that has good design that I can look at? I've been looking at the Cuyahoga project (http://www.martijnboland.com/cuyahoga/).

Thanks for all your help so far.


Top
 Profile  
 
 Post subject: my solution
PostPosted: Tue May 31, 2005 3:22 pm 
Newbie

Joined: Thu May 26, 2005 4:50 pm
Posts: 7
I ended up creating a property of the Device class that used a sql formula.

Here is the interesting piece of my xml file for Device:
Quote:
<property name="MostRecentCalibration" formula="(select max(x.calibration_date) from cal_calibration x where x.device=this.id)" update="false" insert="false" />


Does this seem reasonable?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.