0

I have searched for this and I cannot find the answer. I think I'm missing something obvious or I don't understand Hibernate as well as I thought. I have a project which could access over 100 tables and so I am trying to implement a generic DAO which can read any of these tables and output the result in an excel sheet. Below is a snippit of the code I am using.

SQLQuery query = session.createSQLQuery("SELECT * FROM " + tables + " WHERE " + conditions );
List<Object[]> ob = query.list();
for(Object[] obj : ob ){
    for(int x=0; x<obj.length; x++)
        System.out.println("Col: " + obj[x].toString());}

However, when I print out the objects it gives all I am getting is the first character in the table.

Col1  Col2  Col3
 1      8     8

When the actual values are:

Col1  Col2  Col3
 135   800   867

Have I done something wrong or do I totally misunderstanding how Hibernate works.

Thanks in advanced.

JF

Edit The code I used to output the object has been added as well as results and actual table data.

  • What is the value of tables? What is the value of conditions? How do you display the result and what is displayed? – JB Nizet Oct 13 '14 at 16:25
  • Tables is one of the many tables it could access. It is something the user inputs, same with the conditions (Dangerous I know). For quick testing I was just iterating through the object list and printing out it's value (obj[x].toString() was the exact call). – Jesusfreaks95 Oct 13 '14 at 16:30
  • That doesn't answer my question. Show us the code. And show us the result you get. My guess is that you're getting something like [Ljava.lang.Object;@78308db1, right? – JB Nizet Oct 13 '14 at 16:33
  • Edited original with more information. If this doesn't answer you're question then I apologize because I'm not understanding what you need. – Jesusfreaks95 Oct 13 '14 at 16:47

2 Answers2

0

Try printing it with:

for(Object[] obj : ob ){   
    System.out.println("Row: " + Arrays.toString(obj));
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • I did as you recommended and I receive the same issue. It formats the data a little differently but I still only get the first char of each value in the table. – Jesusfreaks95 Oct 14 '14 at 14:26
  • Try with a plain JDBC statement. You can use session.doWork and execute the statement to check the actual selected column values. – Vlad Mihalcea Oct 14 '14 at 14:48
  • So using doWork gave me the full values of each column. However, the preceding SQL query still gives only the first char. – Jesusfreaks95 Oct 14 '14 at 15:41
  • Session.doWork allows you to execute JDBC so you can figure out if it's Hibernate or the DB that only returns wrong values. – Vlad Mihalcea Oct 14 '14 at 15:46
0

Okay, so I found the issue.

https://hibernate.atlassian.net/browse/HHH-2304

Sums it up and here:

Hibernate native query - char(3) column

shows a solution.

For those who want a summary:

Hibernate has issues when returning type char(>1) and so you have to addScalar or cast your returning values in order to get the full value.

Community
  • 1
  • 1