0

I would like to test SQLQuery of Hibernate API, I managed to do so :

    SQLQuery query = session.createSQLQuery("Select * from table;");

    List<Object[]> l = query.list();
    Iterator<Object[]> it = l.iterator();

    while(it.hasNext()) {
        Object[] o = it.next();
        System.out.println(o[0] + " - " + o[1]);
    }

I managed to get the values in the table, from the two columns. But is there a way to get the column numbers of the tables and also their names?

(I know it's pretty stupid because Hibernate's major benefit is ORM and I'm just here trying to query some unmapped information)

PacDroid
  • 541
  • 1
  • 7
  • 22
  • You can create your own ResultTransformer which has access to array of column aliases if that works for you. – a1ex07 Nov 26 '14 at 17:27

2 Answers2

2

Use following sql query to get columns name :

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'YourTableName'

Bhavin Panchani
  • 1,332
  • 11
  • 17
0

Since hibernate is not managing the entities you cannot ask hibernate of the table metadata, you can execute seperate sql select queries (database specific) to get the table metadata.

Sajan Chandran
  • 11,287
  • 3
  • 29
  • 38