Get dynamic SQL column names from Hibernate

I have an Oracle table that has a CLOB in it. Inside this CLOB there may be an SQL statement. This can be changed at any time.

I am currently trying to dynamically run these SQL statements and return the column names and data back. This should be used to dynamically create a table on a web page.

Using Hibernate, I create a request and get this data:

List<Object[]> queryResults = null;
SQLQuery q = session.createSQLQuery(sqlText);
queryResults = q.list();

This gets the data I need, but not the column names. I tried to use the method getReturnAliases(), but it throws the error message that "java.lang.UnsupportedOperationException: SQL queries do not currently support returned aliases"

So my question is: is there a way using Hibernate to get these values ​​dynamically?

+4
source share
3 answers

You can use:

q.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String,Object>> aliasToValueMapList=query.list();

to get column names in createSQLQuery.

See this question for more details .

+8
source

You can use the addScalar method to determine the columns.

Take a look at 16.1.1 https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html

+1
source

All Articles