How to define record hierarchy with SQL or JPA

I have a system for defining a menu link hierarchy and creating a palette. I have 2 tables: Voceand Voci.

Voceis the name of the menu link and the name of the page name. Vocirepresents a hierarchy Voce. Everyone Vocecan have another Voceas a son (here you do not see the relative code, in any case I use JPA to bind Voce to many Voci). For example, this is the “add many elements” page / link hierarchy:menu --> manage menu --> add many items.

 ----------
|         |
|        \/           Voce
|       -----------------------
|       |id |      name       |    
|       -----------------------
|       |1  |menu             |   
|      -----------------------
|       |2  |manage menu      |  
|       -----------------------
|       |3  |add single item  |
|       -----------------------
|       |4  |add many items   |
|       -----------------------
|
|_____________
        |     |
        |     |
Voci    |     |
-----------------
|id |father|son |
 ----------------
|1  |  1   |  2 |
 ----------------
|2  |  2   |  3 |
-----------------
|3  |  2   |  4 |
-----------------

: " ", ? , ( " " ), , .

JPA ( Spring MVC Hibernate), Voci, " ".

.

+4
1

. depth. .

|parent|child|depth 
|1     |1    |0 
|1     |2    |1
|1     |3    |2
|1     |4    |3

|2     |2    |0 
|2     |3    |1
|2     |4    |2

|3     |3    |0 
|3     |4    |1

|4     |4    |0

, , , , :

select parent from Voci where child = 4 order by depth

returns 4,3,2,1

-> select * from Voce where id in (select parent from Voci where child = 4 order by depth)
   or a correspondent JPA Query

. ( ) , - Voci - - -.

+1

Source: https://habr.com/ru/post/1622734/


All Articles