-->
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 convert complex native SQL query in Hibernate
PostPosted: Wed Aug 16, 2006 3:51 am 
Newbie

Joined: Thu Jul 27, 2006 10:35 am
Posts: 16
Hi All

I am using Hibernate version2.

I have a complex native SQL query which works fine when I execute it in Oracle from Toad or Oracle client.

Now I tried to embed this query in Hibernate's createSQLQuery () but I reckon I am missing some pieces over here. Do guide me on the same.


My Oracle SQL query is something like this
(To avoid confusion I am not posting the complete query just the important parts)

select a.name, sum(a.total), sum(a.open), avg(b.avg_open)
from
(
select AGENT.name,
count(REQUEST.REQUEST_ID) TOTAL,
sum(decode(REQUEST.status,'Open',1,0)) OPEN,
from <<<<AGENT, REQUEST where clause >>>
) a,
(
select AGENT.name,
round(avg(decode(ACTIVITY.status,'Open',ACTIVITY.ACTIVITY_DATE - REQUEST.REQUEST_DATE)),0)
AVG_OPEN
from <<<<AGENT, REQUEST,ACTIVITY where clause >>>
) b
where a.name = b.name
group by grouping sets(a.name,())


Can some one help me in getting this query inside the createSQLQuery ()
syntax.


Regards,


Top
 Profile  
 
 Post subject: native SQL in createSQLQuery
PostPosted: Wed Aug 16, 2006 4:38 am 
Newbie

Joined: Thu Jul 27, 2006 10:35 am
Posts: 16
Hi

Here is what I am using

select a.name as {BR.NAME}, sum(a.total) as {BR.TOTAL}, sum(a.open) as
{BR.SUMOPEN} , avg(b.avg_open) as {BR.AVGOPEN}
from
(
select AGENT.name,
count(REQUEST.REQUEST_ID) TOTAL,
sum(decode(REQUEST.status,'Open',1,0)) OPEN,
from <<<<AGENT, REQUEST where clause >>>
) a,
(
select AGENT.name,
round(avg(decode(ACTIVITY.status,'Open',ACTIVITY.ACTIVITY_DATE - REQUEST.REQUEST_DATE)),0)
AVG_OPEN
from <<<<AGENT, REQUEST,ACTIVITY where clause >>>
) b
where a.name = b.name
group by grouping sets(a.name,())

***********

results = session.createSQLQuery(sqlQuery,new String[] {"BR"},
new Class [] {Request.class,Agent.class,Activity.class}).list();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 4:41 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Can you please post the error message you get?

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject: native SQL in createSQLQuery
PostPosted: Wed Aug 16, 2006 5:38 am 
Newbie

Joined: Thu Jul 27, 2006 10:35 am
Posts: 16
Hi Mike

The error is caused when I am executing the query

****************
results = session.createSQLQuery(
sqlQuery, new String[] {"BR"},
new Class[] {Request.class,Agent.class,Activity.class}
).list();
****************

Here is the stack trace:

**************
java.lang.ArrayIndexOutOfBoundsException
at net.sf.hibernate.loader.SQLLoader.<init>(SQLLoader.java:64)
at net.sf.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:3861)
at net.sf.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:52)

**************

So its not that the query is failing. I think the array being set in my session.createSQLQuery is erroneous

Regards


Top
 Profile  
 
 Post subject: native SQL in createSQLQuery
PostPosted: Wed Aug 16, 2006 5:49 am 
Newbie

Joined: Thu Jul 27, 2006 10:35 am
Posts: 16
Hi

I got around the problem by giving all the aliases which I am using

so this works now
results = session.createSQLQuery(
sqlQuery, new String[] {"BR","a","b"},
new Class[] {Request.class,Agent.class,Activity.class}
).list();


Here is the query which I am using
select a.name as {BR.NAME}, sum(a.total) as {BR.TOTAL}, sum(a.open) as
{BR.SUMOPEN} , avg(b.avg_open) as {BR.AVGOPEN}
from
(
select AGENT.name,
count(REQUEST.REQUEST_ID) TOTAL,
sum(decode(REQUEST.status,'Open',1,0)) OPEN,
from <<<<AGENT, REQUEST where clause >>>
) a,
(
select AGENT.name,
round(avg(decode(ACTIVITY.status,'Open',ACTIVITY.ACTIVITY_DATE - REQUEST.REQUEST_DATE)),0)
AVG_OPEN
from <<<<AGENT, REQUEST,ACTIVITY where clause >>>
) b
where a.name = b.name
group by grouping sets(a.name,())

but now I am getting this error
***************
No column name found for property [NAME]
***************

How can i set the properties for values which are calculated such as TOTAL & SUMOPEN above. I wont be having any mappings for these columns in hbm.xml files.

Regards


Top
 Profile  
 
 Post subject: native SQL in createSQLQuery
PostPosted: Wed Aug 16, 2006 10:14 am 
Newbie

Joined: Thu Jul 27, 2006 10:35 am
Posts: 16
Hi

I searched this forum for some way of handling the aggregate functions like count , sum as stated in the query above.

But it seems most solution like addScalar & return alias are specific to Hibernate version3.0

Can some one give me an option which can work in Hibernate version2.0.

Also do let me know if creating a custom class (just for the select query values) will help in this scenario


Regards


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 1:27 pm 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
Create a mapping similar to following. SQL can be as complex as you need. Remember that the id must be unique for each row returned.
Code:
<hibernate-mapping>
  <class name="TestView">
    <id name="id" type="long">
      <generator class="assigned" />
    </id>
    <property name="value1" update="false" insert="false" type="string" />
  </class>

  <sql-query name="query1">
    <![CDATA[
SELECT 1 AS {tv.id}, 'value' AS {tv.value1} FROM dual
    ]]>
    <return alias="tv" class="TestView"/>
  </sql-query>
</hibernate-mapping>


Add reference to this mapping to configuration.

Create a POJO that corresponds to the mapping.

You should be able to execute using something like this:
Code:
List results = session.getNamedQuery("query1").list();


This should work -- I used this extensively with H2 prior to moving to H3.

Curtis ...

_________________
---- Don't forget to rate! ----


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.