-->
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: How do I list out a select query with several columns??
PostPosted: Tue May 11, 2004 9:37 am 
Regular
Regular

Joined: Tue May 11, 2004 9:23 am
Posts: 69
Pretty new at Hibernate and right now I have create a testdatabase. Want to list out a select query containing several columns. One column works fine but how do I program to list out the answer with several columns?

This is my query & how the answer gets into an iterator:

Query q = session.createQuery("SELECT co.countryCode, st.storeNo FROM net.sf.hibernate.Country co, net.sf.hibernate.Store st WHERE co.countryCode=st.countryCode AND co.countryCode='DK'");

List list = q.list();
System.out.println("Found " + list.size() + " matching search criteria");
Iterator listIter = list.iterator();


Best Reg. Johan


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 10:32 am 
Regular
Regular

Joined: Tue May 11, 2004 9:23 am
Posts: 69
Hey,

I've added some more code. I haven't really figured out how to take care of the [b]"storeNo"[/b] and "countryCode" the the SELECT statement returns.

session = sessionFactory.openSession(); //open sessions

Query q = session.createQuery("SELECT st.storeNo, co.countryCode FROM net.sf.hibernate.Country co, net.sf.hibernate.Store st WHERE co.countryCode=st.countryCode AND co.countryCode='DK'");

List list = q.list();

Iterator listIter = list.iterator();

System.out.println("COUNTRY");
System.out.println("--------");

while(listIter.hasNext()) {
String temp = (String) listIter.next();
System.out.println("StoreNo: " + temp);
}

session.flush();
session.connection().commit();
session.close();


What's wrong with my code? I get this fault:

java.lang.ClassCastException: java.lang.Object
at net.sf.hibernate.Test3.joinCountryStore(Test3.java:83)
at net.sf.hibernate.Test3.menu(Test3.java:56)
at net.sf.hibernate.Test3.main(Test3.java:121)
Exception in thread "main"



Br. Johan


Top
 Profile  
 
 Post subject: Re: How do I list out a select query with several columns??
PostPosted: Tue May 11, 2004 10:42 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Johan,

Code:
Query q = session.createQuery("SELECT co.countryCode, st.storeNo FROM net.sf.hibernate.Country co, net.sf.hibernate.Store st WHERE co.countryCode=st.countryCode AND co.countryCode='DK'");

List list = q.list();
System.out.println("Found " + list.size() + " matching search criteria");
Iterator listIter = list.iterator();


as I rember the list that you got via the code above, should contains elements of Object[2] array type, try the following:
Code:
List list = q.list();
for (Iterator i = list.iterator(); i.hasNext();)
{
    Object[] obj = (Object[]) i.next();
    String countryCode = (String) obj[0];
    Integer storeNo = (Integer) obj[1];
}


I don't remember, but if Object[] obj = (Object[]) i.next(); throws ClassCastException, try List obj = (List) i.next();

Use your debugger with watch i.next().getClass().getName();

Best regards,
Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 2:29 am 
Regular
Regular

Joined: Tue May 11, 2004 9:23 am
Posts: 69
Hey Leonid,

Thx a lot for that. It solved my problem.

/Johan


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.