-->
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.  [ 6 posts ] 
Author Message
 Post subject: Retrieving my objects from a list
PostPosted: Thu Oct 16, 2003 3:46 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 2:18 pm
Posts: 20
I'm having some trouble with this:

Session sess = sessions.openSession(); // obtain a JDBC connection and
// instantiate a new Session

Query q = sess.createQuery(
"select di.drugCode, di.drugName " +
"from PortalDrugItem as di "
);
List result = null;
result = q.list();
System.out.println(result.size());

ArrayList list = new ArrayList();
Iterator iter = list.iterator();

while ( iter.hasNext() ) {
System.out.println("Hi");
PortalDrugItem foo = (PortalDrugItem) iter.next();
System.out.println(foo.getDrugName());
}


for (int i = 0; i < result.size(); i++) {
Object o = result.get(i);
//PortalDrugItem di = (PortalDrugItem) o;

System.out.println(o);
//System.out.println(di.getClass().getName());
}


sess.close();


If I use an iterator, it doesn't iterate anything. The output says the size is 22. I can execute the for loop, but if I cast to PortalDrugItem it throws classcastexception.

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 4:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Ummm, wouldn't you want
Code:
Iterator iter = result .iterator();


instead of

Code:
Iterator iter = list.iterator();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 4:26 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 2:18 pm
Posts: 20
Good call. I did that, now here's my code in that section:

while ( iter.hasNext() ) {
System.out.println("Hi");
Object o2 = iter.next();
PortalDrugItem foo = (PortalDrugItem) o2;
System.out.println(foo.getDrugName());
}


And here's the output:

INFO: Query language substitutions: {}
Hibernate: select portaldr0_.DRUG_CODE as x0_0_, portaldr0_.DRUG_NAME as x1_0_ from PRTL_DRUGS portaldr0_
22
Hi

with the ClassCastException in the returned JSP.
Steve


Top
 Profile  
 
 Post subject: You're not selecting the class
PostPosted: Thu Oct 16, 2003 4:44 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
I think your problem is that you're selecting two columns from your class instead of instances of the class itself. Your iterator is probably returning arrays containing values for the two columns instead of instances of PortalDrugItem. To select the whole object your HQL should be something like:

FROM item IN class PortalDrugItem
WHERE item.field=:value


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 4:45 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 2:18 pm
Posts: 20
Here's my output, from startup to hitting my jsp that does the hibernate.

Oct 16, 2003 4:45:05 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 15983 ms
Oct 16, 2003 4:45:11 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: Hibernate 2.0.2
Oct 16, 2003 4:45:11 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: loaded properties from resource hibernate.properties: {hibernate.connectio
Oct 16, 2003 4:45:11 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
Oct 16, 2003 4:45:11 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: JVM proxy support: true
Oct 16, 2003 4:45:11 PM net.sf.hibernate.cfg.Configuration addClass
INFO: Mapping resource: com/cti/portal/drugorder/PortalDrugItem.hbm.xml
Oct 16, 2003 4:45:12 PM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: com.cti.portal.drugorder.PortalDrugItem -> PRTL_DRUGS
Oct 16, 2003 4:45:12 PM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-many association mappings
Oct 16, 2003 4:45:12 PM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
Oct 16, 2003 4:45:13 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Oct 16, 2003 4:45:13 PM net.sf.hibernate.dialect.Dialect <init>
INFO: Using dialect: net.sf.hibernate.dialect.OracleDialect
Oct 16, 2003 4:45:13 PM net.sf.hibernate.connection.DriverManagerConnectionProvi
INFO: Hibernate connection pool size: 1
Oct 16, 2003 4:45:13 PM net.sf.hibernate.connection.DriverManagerConnectionProvi
INFO: using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@kn
Oct 16, 2003 4:45:13 PM net.sf.hibernate.connection.DriverManagerConnectionProvi
INFO: connection properties: {user=portal4, password=portal4}
Oct 16, 2003 4:45:13 PM net.sf.hibernate.ps.PreparedStatementCache <init>
INFO: prepared statement cache size: 10
Oct 16, 2003 4:45:13 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Use outer join fetching: true
Oct 16, 2003 4:45:14 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Use scrollable result sets: true
Oct 16, 2003 4:45:14 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: JDBC 2 max batch size: 15
Oct 16, 2003 4:45:14 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: echoing all SQL to stdout
Oct 16, 2003 4:45:15 PM net.sf.hibernate.impl.SessionFactoryObjectFactory addIns
INFO: no JDNI name configured
Oct 16, 2003 4:45:15 PM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Query language substitutions: {}
Hibernate: select portaldr0_.DRUG_CODE as x0_0_, portaldr0_.DRUG_NAME as x1_0_ from PRTL_DRUGS portaldr0_portaldr0_.DRUG_NAME as x1_0_ f
22
Hi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 4:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
cutie's exactly correct. either do:

Code:
Query q = sess.createQuery("select di.drugCode, di.drugName from PortalDrugItem as di ");
for (Iterator itr = q.list().iterator(); itr.hasNext(); )
{
    final Object[] vals = (Object[])itr.next();
    final String code = (String)vals[0];
    final String name = (String)vals[1];
    ...
}


or change the query to "from PortalDrugItem as di"


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