Home > Enterprise >  How can i get the liste of all users with roles in spring boot?
How can i get the liste of all users with roles in spring boot?

Time:11-16

I have 3 tables (User(id_user...) - Role(id_role) - users_roles(id_role,id_user) (manytomany)). I want to get the list of all User with Roles.

// Service

public List<User> AllUsers() {
    return loginRepo.findAll();
}

// Controller

@GetMapping("/gestion_utilisateurs")
public String GetAllUsers(Model model) {
    List<User> all_users = loginServ.AllUsers();
    model.addAttribute("all_users", all_users);
    return "administration/identite_patient/gestion_utilisateurs";
}

// View

<th:block th:each="all_users : ${all_users}">
  <tr>
      <td th:style = "${all_users.enabled == true } ? 'color: green' : 'color: red' " > &diams; </td>
      <td th:text="${all_users.nom}"></td>
      <td th:text="${all_users.prenom}"></td>
      <td th:text="${all_users.username}"></td>
      <td th:text="${all_users.roles.role_name}" ></td>
      <td></td>
  </tr>
<th:block>

// User

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "users", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private Integer id;
    private String nom;
    private String prenom;
    private String username;
    private String password;
    private boolean enabled;
    
    
    @ManyToMany(cascade = CascadeType.ALL ,fetch = FetchType.EAGER)
    @JoinTable(
            name = "users_roles",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id")
            )
    
    
    //private Collection<Role> roles = new HashSet<>();
    private List<Role> roles=new ArrayList<Role>(); 


    public User(String nom, String prenom, String username, String password,Boolean enabled, List<Role> roles) {
        super();
        this.nom = nom;
        this.prenom = prenom;
        this.username = username;
        this.password = password;
        this.enabled = true;
        this.roles = roles;
    }


    

        
    
    
    
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "roles")
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "role_id")
    private Long id;
    private String role_name;
    
    public Role(String role_name) {
        this.role_name = role_name;
    }
    
    
    
    
}

I want to show role_name but I don't know how to get it (all_users.roles.role_name doesn't work). Please if someone has an idea.

CodePudding user response:

Since you can have multiple roles in a user, you would need to iterate over them jsut like you are doing with all_users to list all users. So something along the following lines:

<th:block th:each="all_users : ${all_users}">
  <tr>
      <td th:style = "${all_users.enabled == true } ? 'color: green' : 'color: red' " > &diams; </td>
      <td th:text="${all_users.nom}"></td>
      <td th:text="${all_users.prenom}"></td>
      <td th:text="${all_users.username}"></td>
      <td th:text="${all_users.roles.role_name}"></td>
      <td th:each="role : ${all_users.roles}">
          <span th:text="${role.role_name}">
      </td>
      <td></td>
  </tr>
<th:block>
  • Related