I am dealing with one table on the database. The columns are: id, name, parent_id
(parent_id would be an id on the table. if parent_id is null, the record is a parent.)
What would be the best way to copy and create same record through recursive loop from recursive loop so I can create same tree structure as it is:
- item
- item1
- item 1.1
- item 1.1.1
- item 1.1.2
- item 1.2
- item 1.1
- item 2
- item 2.1
- item 2.1.1
- item 2.2
- item 2.1
- item3
- item1
etc and it is like nth level.
You don’t need to use recursion, the code will be clearer if you don’t.
First the result set needs to be ordered by
idandparent_id, then you just iterate through the result set using ajava.util.Mapas an index, something like this: