使用Hibernate,我如何查询表并返回键值对id> name的hashmap?

时间:2022-04-01 16:46:05

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.

我想用hibernate机制查询这个表(使用第二个缓存级别)。 hibernate查询的结果必须是哈希映射,其中键是字段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

谢谢

6 个解决方案

#1


11  

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返回带有hibernate的映射。使用这样的东西:

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

来自hibernate docs:http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select

#2


5  

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查询。

#3


3  

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格式检索查询输出。

#4


0  

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.

与其他答案不同,这应该受益于二级缓存。

#5


0  

Hiii...

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.

编辑:您可以将结果对象用作地图并完成。

#6


-1  

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给出键/值对,这意味着返回地图。

#1


11  

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返回带有hibernate的映射。使用这样的东西:

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

来自hibernate docs:http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select

#2


5  

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查询。

#3


3  

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格式检索查询输出。

#4


0  

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.

与其他答案不同,这应该受益于二级缓存。

#5


0  

Hiii...

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.

编辑:您可以将结果对象用作地图并完成。

#6


-1  

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给出键/值对,这意味着返回地图。