SQL 使用 Hibernate,如何查询表并返回具有键值对 id>name 的哈希图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4736743/
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
With Hibernate, how can I query a table and return a hashmap with key value pair id>name?
提问by Jerome Cance
I've got this table :
我有这张桌子:
table name : Account
Fields : id (varchar), name(varchar), other fields...
I want to query this table with hibernate mechanism (to use the second cache level). The result of the hibernate query must be a hash map where the key is the field id and where the value is the field name.
我想用休眠机制查询这个表(使用第二级缓存)。休眠查询的结果必须是一个哈希映射,其中键是字段 id,值是字段名称。
How can I write it with HQL ?
我如何用 HQL 编写它?
If I use map, I can only use alias and if I use a constructor with an object, I must transform result to hashmap which is time consuming.
如果我使用 map,我只能使用别名,如果我使用带有对象的构造函数,我必须将结果转换为 hashmap,这很耗时。
Example :
Id | name | other fields
1 Jerome ...
2 Steve ...
3 Nick ...
the result of the query must be a hashmap :
查询的结果必须是一个 hashmap :
1>Jerome
2>Steve
3>Nick
thanks
谢谢
回答by tbraun
This question is old but this might still help other people. You can now use HQL to return maps with hibernate. Use something like this:
这个问题很旧,但这可能对其他人有帮助。您现在可以使用 HQL 通过休眠返回地图。使用这样的东西:
select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n ) from Cat cat
From hibernate docs: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select
来自休眠文档:http: //docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select
回答by sblundy
I think the closest you can get is to use this query:
我认为你能得到的最接近的是使用这个查询:
select id, name from Account
which'll give you a result set of two length arrays. You'll have to build the map manually, like so:
这将为您提供两个长度数组的结果集。您必须手动构建地图,如下所示:
for(Object[] row : rs) {
map.put(row[0], row[1]);
}
Note that this will largely ignore the second level cache and get translated into a SQL query.
请注意,这将在很大程度上忽略二级缓存并转换为 SQL 查询。
回答by Ganesh Shirsat
Default entity mode of Hibernate is EntityMode.POJO.
Hibernate 的默认实体模式是 EntityMode.POJO。
you can use EntityMode.MAP entity mode for retrieving the query output in Map format.
您可以使用 EntityMode.MAP 实体模式以 Map 格式检索查询输出。
回答by Ashfak Balooch
Hiii...
喂...
Below code might help you.
下面的代码可能对你有帮助。
Query query = session.createQuery("select id, name from table");
List results = query.list();
To display the results as objects does work:
要将结果显示为对象确实有效:
int i;
int j;
Object object = null;
for (i = 0; i < results.size(); i++) {
System.out.println("-------");
Object[] obj = (Object[]) results.get(i);
for (j=0;j<obj.length;j++)
{
System.out.println(obj[j]);
}
System.out.println("-------");
}
Edit : You can use that results objects as map and you'r done.
编辑:您可以将该结果对象用作地图,然后就完成了。
回答by Alex Godofsky
Assuming for the moment that Account doesn't have any non-lazy associations that would be loaded, the following is just the best you're going to get, performance-wise:
假设目前 Account 没有任何可加载的非惰性关联,以下只是您将要获得的最佳性能:
List<Account> accounts = (List) session.createQuery("from Account").list();
for (Account account : accounts) map.put(account.getID(), account.getName());
This may be "time consuming" but it's not like Hibernate can somehow magically avoid the step of putting each returned row into a map.
这可能是“耗时的”,但它不像 Hibernate 可以以某种方式神奇地避免将每个返回的行放入映射的步骤。
Unlike the other answer, this should benefit from the second-level cache.
与其他答案不同,这应该受益于二级缓存。
回答by Ganesh Shirsat
intead of using default entity name you can applied the entity-name to hbm.xml file.
代替使用默认实体名称,您可以将实体名称应用于 hbm.xml 文件。
for Example
例如
using this, hibernate give the key/value pair that means return the map.
使用这个,hibernate 给出键/值对,这意味着返回地图。