-->
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: JOIN problem - Can't do grouped sum on 4 joined tables
PostPosted: Thu Jan 04, 2007 8:29 am 
Newbie

Joined: Thu Jan 04, 2007 8:10 am
Posts: 3
Location: ireland
Hi there,
I'm using Hibenate 3.2.1 GA with MySQL 5 database and I'm trying to do a grouped sum of 2 columns joined by 4 tables,
its easy to do in the SQL select statement below:

SELECT crate.type, SUM(stackedproduct.no_of_crates) FROM crate, stackedproduct, orderproduct, product
WHERE
stackedproduct.orderproduct_id = orderproduct.id AND
orderproduct.product_id = product.id AND
product.crate_id = crate.id
GROUP BY crate.type;

I can't seem to do this in Hibernate however, I think I am not defining the associations between the tables correctly:

Criteria stackedProductsCriteria = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(StackedProduct.class);
stackedProductsCriteria = stackedProductsCriteria.createCriteria("orderProduct");
stackedProductsCriteria = stackedProductsCriteria.createCriteria("product");
stackedProductsCriteria = stackedProductsCriteria.createCriteria("crate");

stackedProductsCriteria.setProjection(Projections.projectionList()
.add(Projections.groupProperty("orderProduct.product.crate.type"), "type")
.add(Projections.sum("noOfCrates"), "noOfCrates"));

List <CrateStatsBean> list = stackedProductsCriteria.setResultTransformer(Transformers.aliasToBean(CrateStatsBean.class)).list();


But I get the error:
Exception in thread "main" org.hibernate.QueryException: could not resolve property: orderProduct.product.crate.type of: com.myproj.hibernate.pojo.StackedProduct
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1310)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getType(CriteriaQueryTranslator.java:477)
at org.hibernate.criterion.PropertyProjection.getTypes(PropertyProjection.java:36)
at org.hibernate.criterion.AliasedProjection.getTypes(AliasedProjection.java:37)


The member association of the stackedProduct object "orderProduct.product.crate.type" is correct though (at least in java), so it seems that I am not defining the joins between the tables clearly enough for Hibernate. I've tried all kinds of variations and also with .createAlias() for each table and that doesn't do it either. I have been able to do this successfully before joining 2 tables, but I'm can't do it with 4. Does anyone have any ideas?

Thanks for any help/suggestions
Rory


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 1:22 pm 
Regular
Regular

Joined: Thu Aug 17, 2006 4:50 am
Posts: 55
Location: Mallorca
have you tried, with createAlias and then, instead of:

Code:
.add(Projections.groupProperty("orderProduct.product.crate.type"), "type")


use

Code:
.add(Projections.groupProperty("crate.type"), "type")


?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 6:31 am 
Newbie

Joined: Thu Jan 04, 2007 8:10 am
Posts: 3
Location: ireland
Hi Dominicus,
I got in working thanks to you. Here is the working solution I have now:

Code:
      Criteria stackedProductsCriteria = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(StackedProduct.class)
         .createAlias("orderProduct", "orderProduct")
         .createAlias("orderProduct.product", "product")
         .createAlias("product.crate", "crate");
      Criteria deliveryOrderCriteria = stackedProductsCriteria.createCriteria("orderProduct.deliveryOrder");
      deliveryOrderCriteria = createDeliveryOrderCriteria(deliveryOrderCriteria);
   
      stackedProductsCriteria.setProjection(Projections.projectionList()
           .add(Projections.groupProperty("crate.type"), "type")
           .add(Projections.sum("noOfCrates"), "noOfCrates"));
   
       List <CrateStatsBean> list = stackedProductsCriteria.setResultTransformer(Transformers.aliasToBean(CrateStatsBean.class)).list();


I explicitly set all the associations with createAlias() as you can see and then just used "crate.type" in the Projections.groupProperty() as you suggested.
Thanks a lot for your help, I really appreciate it.
Rory


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.