-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to have a list of defined objects with aggregation ...?
PostPosted: Tue Apr 13, 2010 3:51 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
The following is a HQL from chapter 14.14 of the reference documentation
Code:
select order.id, sum(price.amount), count(item)
from Order as order
    join order.lineItems as item
    join item.product as product,
    Catalog as catalog
    join catalog.prices as price
where order.paid = false
    and order.customer = :customer
    and price.product = product
    and catalog = :currentCatalog
group by order
having sum(price.amount) > :minAmount
order by sum(price.amount) desc

If I want to have more order attributes along with the sum and count, how to modify this HQL? I think it should be something like
Code:
select new Order(order.id, ..., sum(price.amount), count(item))
from Order as order ...

where the sum and count can be defined as @Transient attributes in the Order object. But, it won't work.

How to solve this problem?


Top
 Profile  
 
 Post subject: Re: How to have a list of defined objects with aggregation ...?
PostPosted: Wed Apr 14, 2010 5:00 am 
Newbie

Joined: Tue Apr 06, 2010 8:46 am
Posts: 7
Its not a good practice to pollute your domain objects with reporting attributes.
Better make a new pojo i.e. OrderSummary and introduce there properties for sums or counts together with whatever else you need. If your reporting changes you only need to modify those reporting classes.


Top
 Profile  
 
 Post subject: Re: How to have a list of defined objects with aggregation ...?
PostPosted: Wed Apr 14, 2010 5:19 am 
Newbie

Joined: Tue Apr 06, 2010 8:46 am
Posts: 7
Of course don't forget to import it to the hbm file where you define your hql

<import class="your.package.OrderSummary"/>


Top
 Profile  
 
 Post subject: Re: How to have a list of defined objects with aggregation ...?
PostPosted: Wed Apr 14, 2010 1:09 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
grid wrote:
Its not a good practice to pollute your domain objects with reporting attributes.
Better make a new pojo i.e. OrderSummary and introduce there properties for sums or counts together with whatever else you need. If your reporting changes you only need to modify those reporting classes.


In my case, I already created a report domain object which is mapped into a view. It is read-only. Since a query in a view can't take any variables, I can't put any aggregation function in the view. I can get a list of those report objects first and iterate over the list to get those calculated data. I want to have a more effective approach, or a better/right solution for reporting.


Last edited by wei725 on Wed Apr 14, 2010 1:24 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: How to have a list of defined objects with aggregation ...?
PostPosted: Wed Apr 14, 2010 1:12 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
grid wrote:
Of course don't forget to import it to the hbm file where you define your hql

<import class="your.package.OrderSummary"/>


I don't use any hbm XML mapping file any more, but annotation.


Top
 Profile  
 
 Post subject: Re: How to have a list of defined objects with aggregation ...?
PostPosted: Wed Apr 14, 2010 1:52 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
I don't know if this what you are looking for but here are my 2cents.
1)For the requirement of adding more attributes on Order object:-
Since the query uses group-by "order" object itself, have you tried in the projections as
Code:
select order.*, sum(price.amount), count(item)
from Order as order

Or
Code:
select order, sum(price.amount), count(item)
from Order as order

2)Resulting columns in a domain object.
if you succeed some how doing the Step 1, have you considered ResultSetTransformer to copy the data back to your Reporting POJO?

Hope this helps!


Top
 Profile  
 
 Post subject: Re: How to have a list of defined objects with aggregation ...?
PostPosted: Wed Apr 14, 2010 7:04 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
latha1119 wrote:
I don't know if this what you are looking for but here are my 2cents.
1)For the requirement of adding more attributes on Order object:-
Since the query uses group-by "order" object itself, have you tried in the projections as
Code:
select order.*, sum(price.amount), count(item)
from Order as order

Or
Code:
select order, sum(price.amount), count(item)
from Order as order

2)Resulting columns in a domain object.
if you succeed some how doing the Step 1, have you considered ResultSetTransformer to copy the data back to your Reporting POJO?

Hope this helps!


Thanks very much for your hint.

The first approach of the step 1 works, but not the second approach. Although I still need to walk over the returned list and put things together, it is far better than fetching data inside of the loop.

p.s. I need to create aliases for all returned data so that I can use Transformers.ALIAS_TO_ENTITY_MAP.


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