Join two tables in thymeleaf

Viewed 16

I have two tables:

  • userOrganization (userId, OrganizationId)
  • user (userId, name, age, ...) The two tables are linked by userId. I passed to the model via the controller a list of userOrganization. In Thymeleaf, I can easily loop on the users to display the userId: it works now. However, I would also like to display the name of the user table.

Do you have any ideas ? What I have now:

<div>

                        <tr th:each="user : ${users}">
                            <td th:text="${user.id}"></td>

                        </tr>

                    </div>

and what I have done but which does not display anything:

<div>

                        <tr th:each="user : ${users}">
                            <td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>

                        </tr>

                    </div>

and controller


    @GetMapping("/add")
    public String pageCreateCompany(final Model model, Principal principal) {

        if (!model.containsAttribute("companyForm")) {
            CompanyForm companyForm = new CompanyForm();
            model.addAttribute("companyForm", companyForm);
            final User userConnected = (User) ((Authentication) principal).getPrincipal();
            model.addAttribute("users", this.userOrganizationService.getActiveUsersForOrganization(userConnected));
        }

        return "company/createCompanyPage";
    }

thanks in advance :)

1 Answers

It simply worked with :

 <div class="createCompany__container-form-input">
                        <td> Affectation du personnel </td>
                        <th:block th:each="user : ${users}">
                        <input type="checkbox" name="userChecked" th:value="${user.id}"/>
                        <label th:text="${user.id}"></label><label th:text="${user.user.firstName}"></label>
                            <br>


                        </th:block>

Related