Accessing hierarchical data via JDBC [closed]
I have a table Menu that has column id and parent. Can we use normal JDBC (no ORM) to load hierarchical data from the database? Can we have a generic solution f开发者_StackOverflow中文版or all databases?
First, design your database tables to store hierarchies. Secondly, use recursive CTEs (Common Table Expressions) to get the hierarchical data. Without any more information about the structure of your database tables, and the queries used, I do not think it is possible to give a more specific answer.
Note, that the queries eventually used across databases could be different (I haven't attempted verifying recursive CTE support across databases), even though recursive CTEs are a part of the SQL99 specification. So, there might be no generic SQL query, but there is certainly a generic approach that you can adopt.
If your data is in a relational database, and your schema represents it in a hierarchical way, then JDBC can extract it - assuming that you can write the appropriate queries.
I fear that there's nothing generic about. You'll have to write those queries, load the results into objects or data structures, and pass them to your UI for rendering.
精彩评论