-->
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: retrieving results in SQLQuery from Hibernate
PostPosted: Tue Jan 31, 2006 9:33 am 
Newbie

Joined: Tue Jan 31, 2006 8:00 am
Posts: 14
Hi All

I am new to Hibernate.

I am using Hibernate3.jar & Oracle9i database.

This is the query i have which i need to run using Hibernate
****************************************
select a.id ,a.number ,ag.name ,s.title
,gp.ptitle ,(select sum(nvl(rs.amount,0))
from rights ,rights_ext
where rights.id (+)= a.id
and rights.rights_ext_id (+)= right_ext.ser_id) amount
,a.status_id, p.name
from a , ap ,gp ,sa ,s ,ag ,apr, p
where a.id = sa.id
and sa.serid = s.serid
and a.id = ap.id
and ap.pid = gp.pid
and a.statusid <> 1
and a.classid = 3
and astatusid = ag.lkid
and a.id = ap.id
and ap.roleid = 1
and ap.pid = p.pid
****************************************

I want to use createSQLQuery of Hibernate & run this query & not HQL.
I want to know how will i capture this return type which is basically columns from different tables.

I cannot use a single domain object (class which maps to a table) in this scenario.
I get a return type as List. Now how do I retrieve the indvidual column values from this List.

Kindly if possible illustrate with a sample case

Regards


Top
 Profile  
 
 Post subject: retrieving results in SQLQuery from Hibernate
PostPosted: Tue Jan 31, 2006 10:43 am 
Newbie

Joined: Fri Dec 24, 2004 3:16 am
Posts: 5
Hello,

I haven't really executed this type of native query yet, but I would expect hibernate to return a list of tuples (see docs 10.4.1 - Executing queries).

Some example code would be

Code:
List items = session.createSQLQuery(...).list();
Iterator itemIt = items.iterator();
while (itemIt.hasNext()) {
  Object[] item = Object[]itemIt.next();
  Number id = (Number)item[0];
  ...
  String title = (String)item[3];
  ...
}


But of course this kind of code is very vulnerable to changes in the query. You can always create a custom object that represents the result of this query and define the return values in a mapping file (see docs 16.3 - Named SQL queries).

Hope this helps.

Luc Feys


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.