-->
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: Aggregate formulas joining other tables?
PostPosted: Wed Sep 22, 2004 8:31 pm 
Newbie

Joined: Fri Jul 30, 2004 7:07 pm
Posts: 12
Is there a way to have a formula property which computes an aggregate from a joined table?

Example. I have an Order which can have many items. Each item has an amount. I want to have a property of the order which is the cache of the sum amounts of all the items associated with the order.

Both the following approaches fail:
<property name="amountCache"
formula="sum(favor.amount) from Item item, Order order where item.order_id = item.id"

sum(favor.amount) from Item item where item.order_id = id


Hibernate version:
2.1
Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 22, 2004 8:38 pm 
Newbie

Joined: Fri Jul 30, 2004 7:07 pm
Posts: 12
Hmm. Somehow this got posted before I was ready! Here's a better version:


Is there a way to have a formula property which computes an aggregate from a joined table?

Example. I have an Order which can have many items. Each item has an amount. I want to have a property of the order which is the cache of the sum amounts of all the items associated with the order.

Both the following approaches fail:
Code:
       <property name="amountCache"
                       formula="sum(favor.amount) from Item item, Order order where item.order_id = item.id"
                       type="long"/>
                   
       <property name="amountCache"
                       formula="sum(favor.amount) from Item item where item.order_id = id"
                        type="long"/>


This bottoms out into a java.sql.SQLException with malformed SQL. I guess I'm asking Hibernate to do too much?

Is there any other approach to this short of pushing this to a java method?


Hibernate version:
2.1

Name and version of the database you are using:
MySQL 4.0


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 22, 2004 8:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You must use a valid subselect:

Code:
( select sum(order.amount) from Order order where order.item_id = id )


Do not qualify column names from the main entity.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 22, 2004 8:43 pm 
Newbie

Joined: Fri Jul 30, 2004 7:07 pm
Posts: 12
Hmm I tried that at one point.

I'm guessing if you database does not support subselects (mysql 4.0), you will get a SQLException?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 22, 2004 8:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
sure, of course.


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.