简体   繁体   中英

How to retrieve data hierarchically using JPA with many to many join to same table

I have a table(tbl_versions) consisting some versions. And relation between versions are like parent and child. so I put relations in another table(tbl_relations) which has two records (parent_id,child_id) and it has many to many relation with tbl_versions. I want to retrieve data as tree model and at the end have a json file which is in tree model. Any suggestion for how can I select and retrieve with JPQL?

Hierarchical queries are not easy to implement. Generally speaking, writing native SQL is the most powerful way to create a query because by end of the day, JPQL, HQL or whatever will be translated to the native SQL and sent to the Database. Then if you could not write your query (I mean with just one go) with SQL then you would not find a way to write it by JPQL.

But getting back to your concrete issue, databases like Oracle providing mechanism like start with connect by which is not supported by JPQL because the other databases like MySQL don't support that.

My suggestion, in this case, is using one more column to hold hierarchy between records. That column works just as a metadata holder which describes not only the parent-child relationship between two records but also has information about current record's ancestors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM