How to list users and their "Roles"

Asked

Viewed 297 times

1

Hello! I need to list the level of user access on my User Query page. But I can’t access Role data through User. The User entity has the Role attribute that is mapped with the Manytomany annotation, thus:

@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: inserir a descrição da imagem aqui

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

Exit from the Hibernate:

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=?
  • What is your problem?

  • @dhb My problem is the one reported in the photo above: I can’t list users' roles, Thymeleaf returns this string from the photo... What I want is through the user I return the name of the Role that was assigned to it, in case there are two registered ADMIN and SUPPORT. I want to show the role of each user in the above picture table.

  • Can’t you do something like ${user.role.name}? Another option would be to implement the toString method of the Role class.

  • @dhb I can’t, when I try returns the value of the photo. I edited the question and put more details now.

  • Now I think I understand, ${user.roles} returns a list of ROLE and you’re trying to show the list so it won’t look right, or you iterate this list just like you did with users th:each="user : ${user} or creates a method to return a string from the concatenated ROLES. .

  • I understood, but I need to show the Role belonging to each user. Is there no way I can access the attributes of the Role through the relationship? Example, I have another Customer Service entity I can list the customer data in the Customer Service query like -> <td th:text="${customer service.client.razaoSocial}"></td>

Show 2 more comments

1 answer

2


How is a list of ROLE you must iterate, try something like:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.