如何将具有对象作为键的Map列表转换为Map?

huangapple go评论59阅读模式
英文:

How to convert a List of Map that has object as key to Map?

问题

你可以尝试修改你的查询以返回一个Map<Shop, Role>而不是List<Map<Shop, Role>>。这将让你更容易获取键值对。以下是修改后的代码示例:

@Query("SELECT m.key, m.value from User u join u.shopRoleMap m where u.usEmail = :email")
Map<Shop, Role> findByUserEmail(String email);

这样,你就可以直接调用userRepository.findByUserEmail(email)来获取一个Map<Shop, Role>,然后遍历该Map以获取键值对。这应该解决你目前获取0和1作为键的问题。

英文:

I have a repository query that is returning a list of Maps as follows:

@Query(&quot;SELECT new map(key(m), value(m) ) from User u join u.shopRoleMap m where u.usEmail = :email&quot;)
     List&lt;Map&lt;Shop,Role&gt;&gt; findByUserEmail(String email);

The Mapping on User is as follows:

@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    @JoinTable(name = &quot;user_shop_role&quot;,
            joinColumns = @JoinColumn(name = &quot;user_fk&quot;), inverseJoinColumns = @JoinColumn(name = &quot;role_fk&quot;))
    @MapKeyJoinColumn(name = &quot;shop_fk&quot;)
    private Map&lt;Shop, Role&gt; shopRoleMap = new HashMap&lt;&gt;();

On my service I need to get the map as Map&lt;Shop,Role&gt; so that I can get the keys and values correspondingly. The code is as follows:

List&lt;Map&lt;Shop,Role&gt;&gt; shopRole= userRepository.findByUserEmail(email);

        for (Map&lt;Shop,Role&gt; map : shopRole){
            for (Map.Entry&lt;Shop,Role&gt; result : map.entrySet()){
                System.out.println(&quot;The key is: &quot;+result.getKey());
                System.out.println(&quot;And the value is: &quot;+result.getValue());
            }
        }
        }

The result that I get is somewhat bizarre, as I expected result.getKey() would give me Shop as the key. However, I'm getting keys as 0's and 1's.

The key is: 0
And the value is: Shop{id=54, sh_name=&#39;Frefdv&#39;, sh_icon=&#39;icon url&#39;, sh_description=&#39;Fashion Shop&#39;, sh_tag=&#39;metadata&#39;, uuid=&#39;99dba3d5-dfaa-446d-9649-b9b98f422f87&#39;, sh_enabled=&#39;N&#39;, created_at=2020-07-30T15:54:10, updated_at=2020-07-30T15:54:10, created_by=&#39;e0b009ef-27c2-405b-961a-86f199b15167&#39;, updated_by=&#39;e0b009ef-27c2-405b-961a-86f199b15167&#39;}
The key is: 1
And the value is: Role{id=0, roleName=&#39;ADMIN&#39;}
The key is: 0
And the value is: Shop{id=55, sh_name=&#39;fnhfh&#39;, sh_icon=&#39;icon url&#39;, sh_description=&#39;Fashion Shop&#39;, sh_tag=&#39;metadata&#39;, uuid=&#39;e3ccdbdf-aad2-43ba-8331-91b2c2c01853&#39;, sh_enabled=&#39;N&#39;, created_at=2020-07-30T15:54:23, updated_at=2020-07-30T15:54:23, created_by=&#39;e0b009ef-27c2-405b-961a-86f199b15167&#39;, updated_by=&#39;e0b009ef-27c2-405b-961a-86f199b15167&#39;}
The key is: 1
And the value is: Role{id=0, roleName=&#39;ADMIN&#39;}

How can I convert this List&lt;Map&lt;Shop,Role&gt;&gt; to a map where I can get key value pairs?

答案1

得分: 1

被问的HQL实际上返回了List<Map<String, Object>>

选择新地图(key(m), value(m))从User u join u.shopRoleMap m where u.usEmail = :email

new map的语法基本上期望的是值。所以当key(m)和value(m)被传递给它时,key(m)和value(m)都被视为Map的值。地图的键基本上是索引值(0,1,2,...)

您可以在Hibernate文档中详细了解更多信息 -
https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#hql-select-clause

所以,您应该将您的HQL替换为

SELECT entry(m) from User u join u.shopRoleMap m where u.usEmail = :email

这应该返回List<Map.Entry<Shop,Role>>,可以迭代以获取正确的结果。

英文:

The HQL in question is actually returning List<Map<String, Object>>

SELECT new map(key(m), value(m) ) from User u join u.shopRoleMap m where u.usEmail = :email

The syntax for new map basically expects the Values. So when key(m) and value(m) is passed to it, then the key(m) as well as value(m) are treated as Map values. The key to the map is basically the index value (0,1,2,..)

You can read more about it in the Hibernate Documentation -
https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#hql-select-clause

如何将具有对象作为键的Map列表转换为Map?

So, you should be replacing your HQL to

SELECT entry(m) from User u join u.shopRoleMap m where u.usEmail = :email

This should return List<Map.Entry<Shop,Role>> which can be iterated to obtain the correct results.

huangapple
  • 本文由 发表于 2020年7月31日 02:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63179341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定