-->
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: HQL + select specified fields
PostPosted: Thu Mar 04, 2010 12:06 pm 
Newbie

Joined: Thu Mar 04, 2010 12:00 pm
Posts: 1
Hello,

I have a simple HQL query :

Code:
SELECT _food.name, _orderelement.quantity, COUNT(_orderelement.quantity) FROM Order _order inner join _order.orderElements as _orderelement inner join _orderelement.food as _food GROUP BY _food.name,_orderelement.quantity ORDER BY _food.name ASC


This query returns what i want but when I try to get these datas via getHibernateTemplate.fin(myQuery) it returns java.util.List<java.lang.Object>

Suppose I have a bean with the required properties called summary

Code:
return (List<Summary>)getHibernateTemplate().find("SELECT _food.name, _orderelement.quantity, COUNT(_orderelement.quantity) FROM Order _order inner join _order.orderElements as _orderelement inner join _orderelement.food as _food GROUP BY _food.name,_orderelement.quantity ORDER BY _food.name ASC");


I never have a real List<Summary> but always a List<Object>

Does anyone has a solution ?


Top
 Profile  
 
 Post subject: Re: HQL + select specified fields
PostPosted: Thu Mar 04, 2010 2:27 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll need a matching constructor in the Summary class and use HQL something like this:

Code:
SELECT new Summary(_food.name, _orderelement.quantity, COUNT(_orderelement.quantity)) ....


Top
 Profile  
 
 Post subject: Re: HQL + select specified fields
PostPosted: Thu Mar 04, 2010 5:40 pm 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Code:
Criteria criteria = session.createCriteria(YourEntity.class);
//select specified fields...
criteria.setResultTransformer(new AliasToBeanResultTransformer(AnotherClass.class));


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.