Reputation: 25
I'm writing simple task managment app in Spring boot. I've got entity:
@Entity
@Table(name = "project")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "project_id")
private Long projectId;
@Column
private String name;
@OneToMany
@JoinColumn(name = "task_id")
private List<Task> task;
// getters and setter omitted
I try to display this in Thymeleaf in that way:
<div th:each="project : ${projects}">
<div th:text="${project.name}"></div>
<div th:each="task : ${project.task}">
<div th:text="${task.name}"></div>
</div>
</div>
It's shows only projects but I can't see my tasks. What I'm doing wrong ?
Upvotes: 0
Views: 937
Reputation: 1354
This probably happens because project.task is null, therefore there is no iteration. By default, attributes that include complex/custom (usually stored in a different table) objects are not selected from your database when you do a query on the parent object (lazy loading).
You can verify this by a simple check in your HTML, e.g.:
<p th:if="${project.task} == null">Task is indeed null</p>
Quick fix:
@OneToMany(fetch = FetchType.EAGER)
Upvotes: 2