I have a hierarchical data structure with a fixed depth of 4. For a better understanding, let's say the following (just an example):
- The root level is called countries.
- Each country contains an arbitrary number of states
- Each state gains an arbitrary number of counties
- Each environment contains an arbitrary number of cities
Thus, there is always a 1-N relationship between levels.
A very important usecase (taking into account the identifier of the country) is to download all the "content" of the country at once with the least possible impact on database performance.
In the first naive approach, I created 4 entitiy classes in Java, where the "Country" entity contains a list of the "State" type, the "State" entity contains a list of the "County" type, etc. ... But what JPA creates subsequently, of course , not 4 tables, but 7 (4 for entities + 3 for joining between levels due to 1-N). I do not know if this is a good solution, because there are many ways to join the hood.
I also tried to compare the subtypes with their parent types (the city belongs to one county, the county belongs to one state, the state belongs to one country). This leads to 4 tables, but makes it difficult to get all the data at once from the point of view of the application. If I'm not mistaken, I will need 4 different queries instead of one.
? ( , ) ( )?
, ?
JPA Hibernate PostgreSQL.