-->
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: the first select runs ok,but the second select can't run!
PostPosted: Wed Mar 10, 2004 8:10 pm 
Regular
Regular

Joined: Tue Dec 30, 2003 2:35 am
Posts: 85
com.one2many.Author objAuthorRs = null;
com.one2many.Author objAuthorERROR = null;
String strQueryString = "select p from com.one2many.Author as p where p.AUTHORID like :ID";
net.sf.hibernate.Query objQuery = objSession.createQuery(strQueryString);
objQuery.setString("ID","111");
//java.util.Iterator objIteratorRs = objQuery.iterate();
//while(objIteratorRs.hasNext())
//{
// objAuthorRs = (com.one2many.Author)objIteratorRs.next();
// System.out.println(objAuthorRs.getALIAS());
//}
for(java.util.Iterator objIteratorRs = objQuery.iterate();objIteratorRs.hasNext();)
{
objAuthorRs =(com.one2many.Author)objIteratorRs.next();
//objAuthorRs =(com.one2many.Author)objQuery.list().get(0);
System.out.println(objAuthorRs.getALIAS());
}

//strQueryString = "select a.AUTHORID,a.ALIAS from com.one2many.Author AS a WHERE a.AUTHORID LIKE :ID";
strQueryString = "select a.AUTHORID from com.one2many.Author as a where a.AUTHORID like :AUTHORID";
net.sf.hibernate.Query objQueryERROR= objSession.createQuery(strQueryString);
//objQuery = objSession.createQuery(strQueryString);
objQueryERROR.setString("AUTHORID","111");
for(java.util.Iterator objIteratorERROR = objQueryERROR.iterate();objIteratorERROR.hasNext();)
{
objAuthorERROR =(com.one2many.Author)objIteratorERROR.next();
//objAuthorRs =(com.one2many.Author)objQuery.list().get(0);
System.out.println(objAuthorERROR.getALIAS());
}

the first select runs ok,but the second can't runs...,i don't know why?
the second throw the error:
java.lang.ClassCastException

at com.one2many.hibernateselect.main(hibernateselect.java:106)

thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 10:53 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
In your first query you are asking for an object, so the entire object gets returned. In your second query though you are asking for specific fields, which will be returned as n Object if one field or an Object[] array for more than one field. You then need to cast from that Object or Object[] to the correct type that you are expecting (such as String or Integer etc.)


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.