-->
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.  [ 9 posts ] 
Author Message
 Post subject: how to query selected columns and cast it to right datatype
PostPosted: Wed Oct 20, 2004 2:29 pm 
Newbie

Joined: Wed Dec 10, 2003 11:49 am
Posts: 17
Hibernate version:
2.1.6
Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Oracle 9i
The generated SQL (show_sql=true):
select devInfo.serialNumber, devInfo.cleanSerialNumber from com.xerox.hc.model.common.device.DeviceInfo as devInfo where devInfo.cleanSerialNumber ='2UP000001'
Debug level Hibernate log excerpt:

I wanted to select particular columns from a table using hibernate query. I have done that for single column and it works fine. But now i wanted to query 2 columns and it will return an array of objects back. I have a query that seems to be okay. But i am having problem in casting that object[] back to string. Is there something in hibernate that can be used to make this happen. I know it is kind of java question. (How to cast an object array to string or string []). But if hibernate allows you to query selected columns, then i would expect that there must be some easy way to cast it back to appropriate object rather than casting it back to the java object created by using middlegen on map (Which is having all the columns in it).
Any help would be useful.
[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 20, 2004 3:54 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
object[0] = devInfo.serialNumber
object[1] = devInfo.cleanSerialNumber

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: how to query selected columns and cast it to right datatype
PostPosted: Fri Oct 22, 2004 10:09 am 
Newbie

Joined: Wed Dec 10, 2003 11:49 am
Posts: 17
Sorry, i don't understand what you exactly want me to do. What is that devInfo.serialNumber and devInfo.CleanSerialNumber that you are refering to? Also, you are assigning it to object[]. I am getting an object[] back from the query. I need to extract the values of the object[] and assign it to either string[] or to individual string variables.
What you are saying is the other way which is assigning serial number to the object[].

Could you please explain?
Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 10:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
1- show java code, hql query for example
2- read the reference guide

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 10:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
devInfo.serialNumber = (String)object[0];
devInfo.cleanSerialNumber = (String)object[1];

;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: java.lang.ClassCastException
PostPosted: Tue Oct 26, 2004 11:53 am 
Newbie

Joined: Wed Dec 10, 2003 11:49 am
Posts: 17
You can't cast it. You will get java.lang.ClassCastException. I tried your idea and didn't work.
You are getting an object[] back. It is not an object of the class type. It is java.lang.ojbect[].

Any more ideas/help?
Elango


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 26, 2004 12:25 pm 
Regular
Regular

Joined: Tue Sep 28, 2004 6:34 pm
Posts: 50
If you are completely not sure what is returned use any debug tool - it should show you what kind of objects are in object[].

Lukasz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 26, 2004 1:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
You can't cast it. You will get java.lang.ClassCastException. I tried your idea and didn't work.
You are getting an object[] back. It is not an object of the class type. It is java.lang.ojbect[].

You tried which idea? What did not work? Why do you seem so intent upon asking for help but not giving any of the information people ask you to give in order to help you?

Quote:
Any more ideas/help?

Sure! Post some of the information others have asked for so that we *can* help...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 26, 2004 1:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I don't understand why this always seems to be so difficult to understand...

You query and return say two properties of your class, and the result is a list of arrays of length two; now just think about what might be appropriately returned as elements of that array... Might seem to make sense that the first position might correspond to the first property requested and the second correspond to the second. Now, just what might be the types of those returns? Hrrrmmm? The java types for the corresponding property types maybe? Could it be that easy? (hint: yes)


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