O
O
Orkhan Hasanli2018-11-26 21:52:34
Java
Orkhan Hasanli, 2018-11-26 21:52:34

How to get records from related table in thymeleaf?

Hello!
I deduce the table with the data from one table and with it of problems it is not observed. But this Entity is connected to another using @ManyToMany, so there is an additional one. table with additional data. How can I access this table from thymeleaf?

@Entity 
public class Task {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long taskId;

    @ManyToMany
    @JoinTable(name="task_authors",
            joinColumns = @JoinColumn(name="task_id", referencedColumnName="taskId"),
            inverseJoinColumns = @JoinColumn(name="user_id", referencedColumnName="user_id")
    )
    private List<User> userList = new ArrayList<>();
}

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long user_id;

    @ManyToMany(fetch = FetchType.EAGER, mappedBy = "userList")
    private List<Task> taskList = new ArrayList<>();
}

thymeleaf:
<tr th:each="task : ${tasks}">
   <td th:text="${task.taskId}">${task.taskId}</td>
  <!-- здесь нужно вывести данные из таблицы task_authors (ManyToMany)-->
</tr>

PS In ${tasks} nestedList<Task>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-11-27
@azerphoenix

<tr th:each="task : ${tasks}">
   <td th:text="${task.taskId}"></td>
   <td th:each="user : ${task.userList}" th:text="${user}"></td>
</tr>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question