Мне нужно реализовать дерево комментариев, но я не могу получить ответы, чтобы ответить на работу.
Comment.java
...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
@ManyToOne
private Post post;
@ManyToOne
private Comment parent;
@OneToMany(mappedBy = "parent")
private List<Comment> children = new ArrayList<Comment>();
...
PostController.java
model.addAttribute("comments", commentRepository.findAllCommentsByPostIdAndParentIsNull(id));
post.html
<th:block th:each="comment : ${comments}">
<p th:text="${comment.content}">comment</p>
<th:block th:each="child : ${comment.children}">
<div class="child">
<p th:text="${child.content}">comment</p>
</div>
</th:block>
</th:block>
база данных комментариев
ID | CONTENT | PARENT_ID | POST_ID
1 text1 null 1
2 text2 1 1
3 text3 2 1
4 text4 null 1
Вывод, который я хочу
text1
text2
text3
text4
вывод, который я получаю
text1
text2
text4
В основном отвечает наответы не отображаются.
Как мне получить желаемый результат?