-->
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: sql query in criteria
PostPosted: Tue Aug 31, 2010 2:24 am 
Newbie

Joined: Mon Aug 30, 2010 5:26 am
Posts: 3
hi All,

I am using following code for getting distinct question code.

try {
Session session = HibernateUtil.currentSession();
Query query = session.createSQLQuery("SELECT distinct level1_code,level1_questions from TBLTEP_CLIENT order by level1_code");
list = query.list();
System.out.println("========= list Size = " + list.size());
Iterator iter = list.iterator();
if (!iter.hasNext()) {
System.out.println("No question to display.");
}
while (iter.hasNext()) {
TbltepClient tblTepClientObj = (TbltepClient) iter.next();
String questionCode = tblTepClientObj.getId().getLevel1Code();
String question = tblTepClientObj.getLevel1Questions();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
HibernateUtil.closeSession();
}


but on line TbltepClient tblTepClientObj = (TbltepClient) iter.next(); I am getting typecasting error stating object can not be cast to TbltepClient.
I also tried following thing
Query query = session.createSQLQuery("SELECT distinct level1_code,level1_questions from TBLTEP_CLIENT order by level1_code").addEntity(TbltepClient.class);
here I am also geting error.
Please tell me How can I get level1_code and level1_question from object.


Top
 Profile  
 
 Post subject: Re: sql query in criteria
PostPosted: Tue Aug 31, 2010 3:31 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 8:18 am
Posts: 35
Is TBLTEP_CLIENT table contains only two columns?
if not then you have to retrieve through Object array.

Instead of TbltepClient tblTepClientObj = (TbltepClient) iter.next(); this
you can write
Object[] obj=(Object[]) iter.next();
String questionCode = obj[0].tostring();
String question = obj[1].tostring();

check it whether it is working or not.


Top
 Profile  
 
 Post subject: Re: sql query in criteria
PostPosted: Tue Aug 31, 2010 6:47 am 
Newbie

Joined: Mon Aug 30, 2010 5:26 am
Posts: 3
thanks
it is working.


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.