【发布时间】:2020-09-09 18:45:46
【问题描述】:
我有 2 个表 Account 和 Group 都包含层次结构中的数据。
示例 - (仅供参考,我使用的是 PostgresSQL)
组
|------|----------|-------------------|
| id | name | parent_group_id |
|------|----------|-------------------|
| 1 | Group1 | null |
| 2 | Group2 | 1 |
| 3 | Group3 | 2 |
| 4 | Group4 | 1 |
|------|----------|-------------------|
Account
|----|----------|----------|
| id | name | group_id |
|----|----------|----------|
| 1 | Account1 | 1 |
| 2 | Account2 | 1 |
| 3 | Account3 | 2 |
| 4 | Account4 | 3 |
| 4 | Account5 | 4 |
-----|----------|-----------
此帐户和组层次结构可以有很多层次。我想使用 Spring 和 Hibernate 以一种有效的方式获取所有组和帐户。
我希望输出像 -
{"name":"Group1","groups":[{"name":"Group4","groups":[],"accounts":[{"name":"Account5"}]},{"name":"Group2","groups":[{"name":"Group3","groups":[],"accounts":[{"name":"Account4"}]}],"accounts":[{"name":"Account3"}]}],"accounts":[{"name":"Account2"},{"name":"Account1"}]}
我检查了一些文章,但它们不是递归的(意味着组内的组等等)。
【问题讨论】:
-
看起来
JPA不支持获取递归数据。请检查这个答案stackoverflow.com/a/3639538/3503019。
标签: spring hibernate spring-boot