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)

1
  • You can create your own ResultTransformer which has access to array of column aliases if that works for you. Commented Nov 26, 2014 at 17:27

2 Answers 2

2

Use following sql query to get columns name :

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.