-->
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.  [ 4 posts ] 
Author Message
 Post subject: I need help converting a SQL statement to HQL
PostPosted: Thu Apr 06, 2006 9:33 am 
Newbie

Joined: Wed Apr 05, 2006 4:37 pm
Posts: 2
Hibernate version:

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:

I'm not really having trouble with Hibernate, so I didn't bother entering all the above information. I just need help converting the SQL below into an HQL query. I did some looking around on the web and through the manual and I couldn't find anything about case statements in Hibernate.

Code:
select
reportingName,
sum(totalNonMonetaryCount) as totalNonMonetaryCount,
sum(totalCreditCount),
sum(totalCreditAmount),
sum(totalDebitCount),
sum(totalDebitAmount)

from
   (select
      coalesce(reporting_Name,'someName') as reportingName,
      (case when transaction_amount = 0 then 1 else 0 end) as totalNonMonetaryCount,
      
      (case when is_credit = 'N' then 1 else 0 end) as totalCreditCount,
      (case when is_credit = 'N' then transaction_Amount else 0 end) as totalCreditAmount,

      (case when is_credit = 'Y' then 1 else 0 end) as totalDebitCount,
      (case when is_credit = 'Y' then transaction_Amount else 0 end) as totalDebitAmount

   from s_Transaction_e10 tran)

where reportingname is not null
group by reportingName


If you have any questions, or need any additional info, please feel free to ask.

-Brent


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 12:32 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
case is not supported by HQL. You'll have to use SQL for this query.

It is generally recommended that you don't even try to use HQL/Criteria for report-type queries, or indeed for most queries that don't map to objects. Hibernate is not intended as a replacement for JDBC, it's more an enhancement to JDBC: don't be afraid to abandon HQL when it's not suitable.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 6:02 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
Actually both simple case..when and searched case..when is supported by Hibernate 3 - I've used them in HQL myself. Here's the relevant section of the docs: http://www.hibernate.org/hib_docs/v3/re ... xpressions

A subselect in the from clause is not currently supported, but you could probably still create that query if you really want to - post your mapping and we can be more help.

Like tenwit said though - if you're just wanting to do non-OO relational stuff then there's probably no point doing it in HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 10:59 am 
Newbie

Joined: Wed Apr 05, 2006 4:37 pm
Posts: 2
Thanks guys


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