-->
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.  [ 12 posts ] 
Author Message
 Post subject: how to do sum(x*y) in select cllause?
PostPosted: Mon Sep 27, 2004 9:07 pm 
Beginner
Beginner

Joined: Wed May 12, 2004 2:52 pm
Posts: 36
If no expressions in the select are supported, how then can I sum the product of two properties?

The following works:
Code:
select new employeeclub.report.TicketSaleSummaryDto(activity.id, activity.name, activityTicket.quantity, sum(lineItem.quantity), activityTicket.unitCost, sum([b]lineItem.unitCost[/b]))
from
    Activity activity,
    ActivityTicket activityTicket,
    LineItem lineItem
where  activityTicket.id = lineItem.item.id group by activity.id, activity.name, activityTicket.quantity, activityTicket.unitCost


but the following does not:
Code:
select new employeeclub.report.TicketSaleSummaryDto(activity.id, activity.name, activityTicket.quantity, sum(lineItem.quantity), activityTicket.unitCost, sum([b]lineItem.unitCost * lineItem.quantity[/b]))
from
    Activity activity,
    ActivityTicket activityTicket,
    LineItem lineItem
where  activityTicket.id = lineItem.item.id group by activity.id, activity.name, activityTicket.quantity, activityTicket.unitCost




Here, lineItem.unitCost is of type Money, lineItem.quantity is of type int.

I receive the following error:

Code:
org.springframework.orm.hibernate.HibernateSystemException: no appropriate constructor in class: employeeclub.report.TicketSaleSummaryDto; nested exception is net.sf.hibernate.PropertyNotFoundException: no appropriate constructor in class: employeeclub.report.TicketSaleSummaryDto
net.sf.hibernate.PropertyNotFoundException: no appropriate constructor in class: employeeclub.report.TicketSaleSummaryDto


I have no idea what sum(lineItem.unitCost * lineItem.quantity) would be returned as. How can you tell? It would be nice if Hibernate provided the method signature it was looking for. I have tried Integer, int, Long, long, Double, double, BigDecimal, Object and Money. No dice. How can I tell what Hiberate resolves this as?

Regards,

Joshua


Top
 Profile  
 
 Post subject: Problem seems to lie with sum(o.x * o.y)!
PostPosted: Mon Sep 27, 2004 9:37 pm 
Beginner
Beginner

Joined: Wed May 12, 2004 2:52 pm
Posts: 36
If I change sum(o.x * o.y) to sum(o.x + o.y) or any other operator, the query works. Is this a bug? Is there a work around?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 5:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
What about "this is not supported" don't you understand? You can't do this in HQL. Do it in java, map a formula property or something else.


Top
 Profile  
 
 Post subject: Why are you so grumpy?
PostPosted: Tue Sep 28, 2004 7:34 am 
Beginner
Beginner

Joined: Wed May 12, 2004 2:52 pm
Posts: 36
Michael,

Why are you so grumpy?

To answer your question, I am still in shock that such a highly toughted persistance framework does not support something as basic as sum(obj.x*obj.y) in the select clause. In addition, the error message I get back "no appropriate constructor in class" does little to suggest that this operation is not supported.

Regards,

Joshua


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 7:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It is not supported because it is very difficult to figure out the JDBC type of such an expression, when we read back the result set.

What type is sum( float + int ) ? Is it constant across all dbs?

Anyway, I think we will be able to implement some support for this with the new query parser in HB3, but I'm concerned that different DBs do not have the same rules for type coercion, etc.

FYI, Hibernate was one of the first, if not -the- first ORM solution to support aggregation *at all*. Most JDO implementations, for example, don't support aggregation, because JDOQL doesn't define it. So the tone of your criticism was a bit unnecessary.

And calling missing features "bugs" is irritating and inexact. Please don't do that in future. Thanks.


Top
 Profile  
 
 Post subject: Thanks gavin
PostPosted: Tue Sep 28, 2004 8:22 am 
Beginner
Beginner

Joined: Wed May 12, 2004 2:52 pm
Posts: 36
Thanks gavin,

Now I understand the problem at hand and realize that this is a missing feature, not a bug.

Regards,

Joshua


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 8:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
ok, np :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 9:00 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Use view, it is a good way for many reasons and this missing feature is not a problem (if nobody makes you to use some incomplete RDBMS )


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 9:02 am 
Beginner
Beginner

Joined: Wed May 12, 2004 2:52 pm
Posts: 36
Interesting idea. Do I need to map an object to this view to get it to work or is there a way I can hack the hql above?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 9:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Use a view which contains the additional calculated column instead of the table you have maped lineitem to now, I'd say


Top
 Profile  
 
 Post subject: A work around without view
PostPosted: Wed Oct 05, 2005 2:11 pm 
Newbie

Joined: Sun Jul 11, 2004 12:14 pm
Posts: 2
Seems sum (x * y) is still not supported directly in HB3. Here's a work around (in MySQL):

assume
x = decimal(20,2)
y = int(10)

sum (x * y) returns Integer, which is propably not what you want. But since you know that there's a maximum of 2 decimal places, you can use sum (x * 100 * y) / 100, and HB3 is smart enough to return a Double.

Takes some hard work to figure out that it's not supported though.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 4:15 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 4:32 am
Posts: 29
Location: Cluj-Napoca
I have had the same problem and I solved it in the next way:

I created into the class a new field:

/**
* The total price per stock of the invoice lines.
*/
private double totalPricePerStock;

/**
* @hibernate.property formula="FAR_AANTAL*FAR_PRIJS_PER_STUK"
*
* @return Returns the totalPricePerStock.
*/
public final double getTotalPricePerStock() {
return totalPricePerStock;
}

/**
* @param totalPricePerStock The totalPricePerStock to set.
*/
public final void setTotalPricePerStock(final double totalPricePerStock) {
this.totalPricePerStock = totalPricePerStock;
}


And into the query I used sum(totalPricePerStock).

For doing this mapping using Xdoclet you need the 1.2.3 version.
The version 1.2 does not generate the mapping with formula.


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