如何从 Java 中的 Hibernate 查询结果中获取列名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14297443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get column names from Hibernate query result in Java?
提问by Frank
I'm writing a Java app that uses Hibernate to get data, in my app I have an input text area that takes an user typed-in sql command string and run that through Hibernate to get whatever data the user queries, so I don't know beforehand what the result table might look like and therefore don't know the column names, but I do need to display user query result in a table with column names relate to the data fields, how to achieve that in Hibernate ? I've tried the following code :
我正在编写一个使用 Hibernate 获取数据的 Java 应用程序,在我的应用程序中,我有一个输入文本区域,它接受用户输入的 sql 命令字符串并通过 Hibernate 运行它以获取用户查询的任何数据,所以我不事先不知道结果表可能是什么样子,因此不知道列名,但我确实需要在列名与数据字段相关的表中显示用户查询结果,如何在 Hibernate 中实现?我试过以下代码:
Session session=HibernateUtil.getSession();
session.beginTransaction();
Query q=session.createQuery(hql);
AliasToEntityMapResultTransformer INSTANCE=new AliasToEntityMapResultTransformer();
q.setResultTransformer(INSTANCE);
List<Map<String,Object>> aliasToValueMapList=q.list();
for (Map<String,Object> map : aliasToValueMapList)
for (Map.Entry<String,Object> entry : map.entrySet()) System.out.println(entry.getKey()+" - "+entry.getValue());
It gave me the following error message : Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sakila.entity.Actor cannot be cast to java.util.Map
它给了我以下错误消息:线程“AWT-EventQueue-0”中的异常 java.lang.ClassCastException: sakila.entity.Actor cannot be cast to java.util.Map
It's pointing to the 1st for loop, since I'm new to Hibernate, don't know if it's doable in it, how to fix the above code ? Could someone show me some sample code that works in my case ?
它指向第一个 for 循环,因为我是 Hibernate 的新手,不知道它是否可行,如何修复上面的代码?有人可以向我展示一些适用于我的案例的示例代码吗?
Edit : As Marcel St?r mentioned below, I need to be able to allow both situations to happen and not limit users' ability to query any data, what's the best way to do it ?
编辑:正如 Marcel St?r 在下面提到的,我需要能够允许这两种情况发生并且不限制用户查询任何数据的能力,最好的方法是什么?
采纳答案by Marcel St?r
I don't quite understand but still dare to answer...
不是很懂,但还是敢回答。。。
If you use HQLlike this Query q=session.createQuery(hql);
suggests you get back objectsand not individual fields by default. This means you're out of luck anyway trying to map back the results to the query BUT you can simply use the object's field names as column names.
如果您像这样使用HQL,则Query q=session.createQuery(hql);
建议您默认返回对象而不是单个字段。这意味着无论如何尝试将结果映射回查询都不走运,但是您可以简单地使用对象的字段名称作为列名称。
If what you get from the text area is plain SQL though, then you have to use session.createSQLQuery(sql)
. What you get back is a list of object arrays. However, here too you only get the data. You'd have to prohibit your users to use select *
queries. Then you can use the name of the field/column in query as your output column names.
如果您从文本区域获得的是普通 SQL,那么您必须使用session.createSQLQuery(sql)
. 你得到的是一个对象数组列表。但是,在这里您也只能获取数据。您必须禁止您的用户使用select *
查询。然后您可以在查询中使用字段/列的名称作为输出列名称。
回答by mindas
Does the user type in the SQL or HQL query? There's a big difference between those.
用户输入的是 SQL 还是 HQL 查询?这之间有很大的区别。
If the user types in HQL query, you can call hqlQuery.getReturnTypes()
, and then for each type you can do whatever suggested in this postto find out the table metadata.
如果用户输入 HQL 查询,您可以调用hqlQuery.getReturnTypes()
,然后对于每种类型,您可以执行本文中建议的任何操作来查找表元数据。
回答by bbones1967
I've got the same kind of problem for HQL query like
我有类似的 HQL 查询问题
select new Map(id as id, name as name) from Person
that I use as view DTO
我用作视图 DTO
With one or more records I can iterate through the map that is element of List<Map<String, Object>
.
The problem only when I need to manage situation with emptydataset. For that case
something like
通过一个或多个记录,我可以遍历作为List<Map<String, Object>
. 只有当我需要用空数据集管理情况时才会出现问题。对于那种情况,类似
public List<String> getAliases(String queryString) {
ArrayList<String> list =
new ArrayList<String>(Arrays.asList(queryString.split("as ")));
List<String> result = new ArrayList<String>();
list.remove(0);
for (String str : list) {
StringTokenizer st = new StringTokenizer(str, ",) ");
result.add(st.nextToken());
}
return result;
}