-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate Query to GROUP BY HOUR
PostPosted: Fri Nov 20, 2015 11:04 am 
Newbie

Joined: Wed Nov 28, 2012 7:37 am
Posts: 4
Hi,

I would like some help with a Hibernate Query. To provide some background, I have a SQL Server database table, called X_COUNTS, that looks like this...

Code:
state_change_time          picks_complete    storeid
2015-06-30 16:25:43.0    325                    8953
2015-06-30 14:37:32.0    289                    8953


I have built the SQL Server query that returns the results that I am after...

Code:
SELECT DATEPART(HOUR, state_change_time) AS HR, SUM(picks_complete) AS ITEMS_PICKED
FROM DBO.X_COUNTS
WHERE storeid = '8953'
GROUP BY DATEPART(HOUR, state_change_time);


What I am having a big struggle with is how to translate this into HQL - obviously the result from my query does not map to the Bean I have used to model the X_COUNTS table. What I have been trying is something like...

Code:
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();

Query query = session.createQuery(<HOW_DO_I_DO_THIS_QUERY_PLEASE?>);


Any advice, example queries would be very helpful - I haven't found anything that quite matches what I am after on the forums, online tutorials etc

Thank you


Top
 Profile  
 
 Post subject: Re: Hibernate Query to GROUP BY HOUR
PostPosted: Mon Nov 23, 2015 9:52 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
Hi diem

Hibernate supports register custom functions in the global configuration, this functionality can let you to use any functions in HQL/JPQL(Though NativeQuery can do it, implement it by HQL/JPQL is more cool).

Please see org.hibernate.cfg.Configuration.addSQLFunction


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