Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:40:10+00:00 2026-06-17T19:40:10+00:00

I am dealing with one table on the database. The columns are: id, name,

  • 0

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 2
      • item 2.1
        • item 2.1.1
      • item 2.2
    • item3

etc and it is like nth level.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:40:11+00:00Added an answer on June 17, 2026 at 7:40 pm

    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 id and parent_id, then you just iterate through the result set using a java.util.Map as an index, something like this:

    Map<Object, Item> items = new HashMap<Object, Item>(); 
    
    if (resultSet.next()) 
    {
      Item root = createItem(resultSet);
      items.put(root.getId(), root);
    
      while (resultSet.next()) 
      {
        Item item = createItem(resultSet);
        items.put(item.getId(), item);
    
        Item parent = items.get(item.getParentId()); 
        if (parent != null) 
        {
          parent.addChild(item);
        }
        else
        {
          ...
        }
      }
    
      return root;
    }
    else 
    {
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When dealing with a denormalised (legacy) database table, whereby you have columns such as:
I have one database table dealing with users login totals and another table dealing
I'm dealing with an annoying database where one field contains what really should be
It's fairly obvious how to model a database table that would act as an
To give a simple example of my problem: //Create database table allowing nulls. CREATE
I am dealing with a legacy application and the database is in Oracle. One
I just seem to hit one error after another when dealing with larger projects
I need some help dealing with three Threads in Android One thread is the
Other questions dealing with use of match_parent pre 2.2 have answers stating one should
I am currently dealing with code purchased from a third party contractor. One struct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.