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