从Hibernate获取动态SQL列名

时间:2022-09-23 21:36:56

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

我有一个Oracle表,里面有一个CLOB。在这个CLOB里面可以是一个SQL语句。这可以随时更改。

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

我目前正在尝试动态运行这些SQL语句并返回列名和数据。这用于在网页上动态创建表。

Using Hibernate, I create the query and get the data like so:

使用Hibernate,我创建查询并获取如下数据:

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

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

这将获取我需要的数据,但不会获取列名称。我尝试使用getReturnAliases()方法,但它抛出一个错误“java.lang.UnsupportedOperationException:SQL查询当前不支持返回别名”

So my question is: Is there a way through Hibernate to get these values dynamically?

所以我的问题是:Hibernate有没有办法动态获取这些值?

3 个解决方案

#1


9  

You can use :

您可以使用 :

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

to get column names in createSQLQuery.

在createSQLQuery中获取列名。

For more details please refer to this question.

有关详细信息,请参阅此问题。

#2


1  

You can use the addScalar method to define the columns.

您可以使用addScalar方法来定义列。

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

请看16.1.1 https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html

#3


1  

You could implement a ResultTransformer ( http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/transform/ResultTransformer.html ) and set it on the native query. I think with a native SQL query you get the aliases as specified in the SQL as alias parameter in the callback method.

您可以实现ResultTransformer(http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/transform/ResultTransformer.html)并在原生查询上设置它。我认为使用本机SQL查询可以获得回调方法中SQL as alias参数中指定的别名。

#1


9  

You can use :

您可以使用 :

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

to get column names in createSQLQuery.

在createSQLQuery中获取列名。

For more details please refer to this question.

有关详细信息,请参阅此问题。

#2


1  

You can use the addScalar method to define the columns.

您可以使用addScalar方法来定义列。

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

请看16.1.1 https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html

#3


1  

You could implement a ResultTransformer ( http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/transform/ResultTransformer.html ) and set it on the native query. I think with a native SQL query you get the aliases as specified in the SQL as alias parameter in the callback method.

您可以实现ResultTransformer(http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/transform/ResultTransformer.html)并在原生查询上设置它。我认为使用本机SQL查询可以获得回调方法中SQL as alias参数中指定的别名。