How to list users and their "Roles"

0

Hello! I need to list users' access level on my Users Lookup page. But I can not access Role data through User. The User entity has the Role attribute that is mapped with the ManyToMany annotation, like this:

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;

Thymeleaf:

                            <table class="table table-striped table-hover" id="lista">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Nome</th>
                                    <th>E-mail (Nome de usuário)</th>
                                    <th>Nível de Acesso</th>
                                    <th>Deletar</th>
                                 </tr>
                            </thead>
                            <tbody>
                                <tr th:each="user : ${user}">
                                    <td th:text="${user.id}"> </td>
                                    <td th:text="${user.name} + '  ' + ${user.lastName}"> </td>
                                    <td th:text="${user.email}"> </td>
                                    <td th:text="${user.roles}"></td>

But when I try to return the name of Role in my view, it looks like this:

How do I show the Role of each user in this table?

Hibernate output:

Hibernate: select user0_.user_id as user_id1_4_, user0_.active as active2_4_, user0_.email as email3_4_, user0_.last_name as last_nam4_4_, user0_.name as name5_4_, user0_.password as password6_4_ from users user0_ where user0_.email=? 
Hibernate: select user0_.user_id as user_id1_4_, user0_.active as active2_4_, user0_.email as email3_4_, user0_.last_name as last_nam4_4_, user0_.name as name5_4_, user0_.password as password6_4_ from users user0_ order by user0_.user_id asc limit ?
Hibernate: select roles0_.user_id as user_id1_3_0_, roles0_.role_id as role_id2_3_0_, role1_.role_id as role_id1_2_1_, role1_.role as role2_2_1_ from user_role roles0_ inner join role role1_ on roles0_.role_id=role1_.role_id where roles0_.user_id=?
Hibernate: select roles0_.user_id as user_id1_3_0_, roles0_.role_id as role_id2_3_0_, role1_.role_id as role_id1_2_1_, role1_.role as role2_2_1_ from user_role roles0_ inner join role role1_ on roles0_.role_id=role1_.role_id where roles0_.user_id=?
    
asked by anonymous 12.09.2017 / 20:02

1 answer

1

As a list of ROLE you should iterate, try something like:

<span th:each="role: ${user.roles}"> 
<td th:text="${role.role}"></td> 
</span>
    
12.09.2017 / 22:11