-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to get retrive the data from quer.list()
PostPosted: Thu Apr 16, 2009 1:19 am 
Newbie

Joined: Tue Oct 23, 2007 4:00 am
Posts: 9
Hi
I am not able to retrive the data from Object[]. Below is the code
StringBuffer q = new StringBuffer("select T.CLI_ID, " +
"T.CLI_ADDR_TYP_CD " +
"from " +
" TABLE T, TABLE2 T2" +
"where " +
"T2.cli_id = T.CLI_ID " +
"and " + "T2.id='100');
query = session.createSQLQuery(q.toString());
List plist = query.list();
Object obj[] = (Object[]) plist.get(0);
if (obj != null) {
for(int i=0; i<obj.length;i++){
System.out.println("DATA>>"+ obj[i]);
}
}
I have three records for above sql result. and i want to get all records from the list.
Here in sysout,i could only see the only one charcter of the coloumn data,
Request you to help me.

Babu.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 5:01 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Sorry double post, cause of bad internet connection.


Last edited by Urmech on Thu Apr 16, 2009 5:31 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 5:03 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi,

have you checked, if you really get an Array of Objects?
You won't get a CalssCastException, cause Object[] and Object are both only simple references.

"plist.get(0)" may return an Object of your Class that represents your
TABLE T.

Use this to check the type.
Code:
System.out.println(plist.get(0)getClass().getName());


Also the implicit call of toString() on Object, may not be appropriate. "System.out.println("DATA>>"+ obj[i]); "

"T.CLI_ADDR_TYP_CD" may be another Array of objects.

Hope this helps.

Greetings Michael


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 5:34 am 
Newbie

Joined: Tue Oct 23, 2007 4:00 am
Posts: 9
' [Ljava.lang.Object;' is the result which i got from sysout you provided, I believe this is Object array. I have the CLI_ID of 10 digit but in sysout it is showing only 1 digit the same case is with the CLI_ADDR_TYP_CD where it shows only 1 character.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 6:58 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi, yeah this indicates an Objectarray.
Also I was wrong in the above post, you should get an ClassCastExecption if it is not an array. What I ment is that in the below example: "o = array;" would not raise an exception.


Code:
Object o = new Object();

Object[] array = new Object[0];

System.out.println(array.length);
System.out.println(o);
System.out.println(o.toString());

System.out.println(array);

produces this output.
Quote:
0
java.lang.Object@7cbde6
java.lang.Object@7cbde6
[Ljava.lang.Object;@1977b9b


as expected:
Quote:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())


So it is quite likely that the objects in your for-clause are another type, which had overriden toString(), cause you don't get the above results.

So you should check this types.
E. G.:
Code:
for(int i=0; i<obj.length;i++){
System.out.println(obj[i].getClass().getName());
System.out.println("DATA>>"+ obj[i]);
}



Greetings Michael


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 7:53 am 
Newbie

Joined: Tue Oct 23, 2007 4:00 am
Posts: 9
Here is the console output :-

SystemOut O java.lang.Character
SystemOut O DATA>>1
SystemOut O java.lang.Character
SystemOut O DATA>>E

I dont understand why it is showing Character obj. It has to be the String type. I am in confusion now, the same code i had already used and that is working fine. Its really mess with me. The data type for the DB field is also varchar. Please suggest.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 8:35 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi,

how does your mappings look like?

If you try to read an existing database, without mapped classes you may have an look here:
http://www.hibernate.org/hib_docs/tools/reference/en/html/reverseengineering.html

But I have no experience with that. Otherwise varChar to string should be default.

Your Id normally shouldn't be a String, unless you use a natural key. So normally I would expect an Object array, with an subclass of Number in first place and a String in second place.

How big are the varchars in your database? What is the expected result?

You don't use an oracle database?

5.2.2. Basic value types (Hibernate manual)
Quote:
string
A type mapping from java.lang.String to VARCHAR (or Oracle VARCHAR2).


Greetings Michael


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