File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes problem in retrieving data with HQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem in retrieving data with HQL" Watch "problem in retrieving data with HQL" New topic

problem in retrieving data with HQL

Sujoy Mukherjee
Ranch Hand

Joined: Dec 31, 2005
Posts: 51

I am new to Hibernate. I am using Netbeans 6.9, Oracle 10G. I want to retrieve data from a table and want to display it.
My table named Product has 3 columns namely Pid, Pname, Price.
Here is my code:

And this is the output:

I don't know why istead of showing the data from the table it is showing the hashcode?
Please help.>
Bill Gorder

Joined: Mar 07, 2010
Posts: 1682

Well you are not using parameterized types but lets assume your array list is an ArrayList<Product>. You need to override toString on your Product object. What you are seeing is the default implementation of toString() on the Object class. See the java doc

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())

This is not really an ORM issue so I am going to move this to the beginning java forum where you can get further assistance with this if you need it.

[How To Ask Questions][Read before you PM me]
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179

Basically you have got a List<Products> , then you loop around this and take out individual Products and you are printing the final Object. By default, the object's toString() method is Overridden to print the hashcode only. If you are expecting to print out the data when you print the object, then you need to Override the toString() method for the Products class and put your logic for the output.

This should work for you.


Programming is about thinking, NOT coding
I agree. Here's the link:
subject: problem in retrieving data with HQL
It's not a secret anymore!