Spring和Hibernate中的分层数据获取

问题描述 投票:0回答:1

我有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"}]}

我已经检查了一些文章,但是它们不是递归的(意味着在一个组内分组,依此类推)。

我有2个表Account和Group都包含一个层次结构中的数据。示例-(仅供参考,我使用的是PostgresSQL)组| ------ | ---------- | ------------------- | | id |名称| ...

spring hibernate spring-boot
1个回答
0
投票

这是Blaze-Persistence的完美用例。

© www.soinside.com 2019 - 2024. All rights reserved.