Fred Kibuchi
Fred Kibuchi

Reputation: 123

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

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

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

The Mapping on User is as follows:

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

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

List<Map<Shop,Role>> shopRole= userRepository.findByUserEmail(email);

        for (Map<Shop,Role> map : shopRole){
            for (Map.Entry<Shop,Role> result : map.entrySet()){
                System.out.println("The key is: "+result.getKey());
                System.out.println("And the value is: "+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='Frefdv', sh_icon='icon url', sh_description='Fashion Shop', sh_tag='metadata', uuid='99dba3d5-dfaa-446d-9649-b9b98f422f87', sh_enabled='N', created_at=2020-07-30T15:54:10, updated_at=2020-07-30T15:54:10, created_by='e0b009ef-27c2-405b-961a-86f199b15167', updated_by='e0b009ef-27c2-405b-961a-86f199b15167'}
The key is: 1
And the value is: Role{id=0, roleName='ADMIN'}
The key is: 0
And the value is: Shop{id=55, sh_name='fnhfh', sh_icon='icon url', sh_description='Fashion Shop', sh_tag='metadata', uuid='e3ccdbdf-aad2-43ba-8331-91b2c2c01853', sh_enabled='N', created_at=2020-07-30T15:54:23, updated_at=2020-07-30T15:54:23, created_by='e0b009ef-27c2-405b-961a-86f199b15167', updated_by='e0b009ef-27c2-405b-961a-86f199b15167'}
The key is: 1
And the value is: Role{id=0, roleName='ADMIN'}

How can I convert this List<Map<Shop,Role>> to a map where I can get key value pairs?

Upvotes: 3

Views: 748

Answers (1)

SKumar
SKumar

Reputation: 2030

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

enter image description here

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.

Upvotes: 1

Related Questions