-->
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: Order/Filter by calculated fields.
PostPosted: Fri May 26, 2006 3:51 pm 
Beginner
Beginner

Joined: Mon Mar 20, 2006 7:59 am
Posts: 30
Hello,

is it possible to order in a criteria by a calculated field? When I want to display for example a list of invoices ordered by invoice total.

The field total is beeing calculated and not persisted in the database. When I try to add an order to it via criteria nhibernate cannot resolve the property. It is clear - because it is not persisted. But it would be nice though to be able to add orders to that kind of fields.

Further I would like to be able to filter by such calculated fields. In the former example let me assume I was interested in the invoices with a total sum of more than 1000€. How would you do that?

Regards,
Sir-Archimedes


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 27, 2006 2:04 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
i don't know if this is the easiest way but...

Quote:
is it possible to order in a criteria by a calculated field? When I want to display for example a list of invoices ordered by invoice total.


create a class specifically for this purpose called something like InvoiceSummary that implements IComparable:

Code:
public class InvoiceSummary(int invoiceID, Long totalAmount) : IComparable
{
   ...
   public int CompareTo(object o)
   {
      InvoiceSummary is = (InvoiceSummary) o;
      return this.totalAmount.CompareTo(is.TotalAmount);
   }
}


then, use HQL (IQuery) to instantiate the class:

Code:
select new InvoiceSummary(i.id, sum(i.products.cost))
   from Invoice i
   group by invoice.id


this will give you an IList of InvoiceSummary objects that you can access through ArrayList.Adapter() to call Sort() on for the ordering. Complex, sorting is done in code instead of the DB, but it works. if you want to do it entirely in the DB, then i think you'll have to use native SQL...

Quote:
Further I would like to be able to filter by such calculated fields. In the former example let me assume I was interested in the invoices with a total sum of more than 1000€. How would you do that?


modify the method that wraps the HQL to accept a minValue and integrate that into the HQL:

Code:
select new InvoiceSummary(i.id, sum(i.products.cost))
   from Invoice i
   group by invoice.id
      having sum(i.products.cost) > 1000.00


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 27, 2006 10:25 am 
Beginner
Beginner

Joined: Mon Mar 20, 2006 7:59 am
Posts: 30
Thank you for the answer. I already knew that I could solve such problem with workarounds like yours. But I was searching for a generic solution. In my specific problem I have a class Tasks. A task consists of several actions. Departments or/and users may be assigned to each action. Now I can calculate the type of responsibility for every action. That lets me calculate a particular user's responsibility for the whole task.

Now I want to filter the tasks to display only tasks that are somehow connected to a user. I think you can solve this also with several subselects via SQL (and so also with HQL) but I thought it might be possible to define it just as a criteria. I thought nhibernate recognize that this property (CurrentResponsibility) is not persisted and then just fetches lines from the database (or the cache) and filters or orders on its own.

But I guess it doesn't work that way?!


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.