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 8883539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:50:11+00:00 2026-06-14T20:50:11+00:00

My questions i about storing one-to-many relationship to database (as Programming a one-to-many relationship

  • 0

My questions i about storing one-to-many relationship to database (as Programming a one-to-many relationship)

Lets assume the example: we have parent and children. Each parent record can have many childs.

// Db storage:
// id name

public class Parent {
    Set<Childs> mChilds;
    private long mId;
    private String mName;
    // etc

    public Parent(String name) {
        this.mName = name;
    }

    public static Parent load(id) {
        // query database for all Parent attributes
        Parent p = new Parent("somename");

        // now its time to load set of child (1)
        Set<Child> children = Child.loadChildren(p);
    }
}

// Db storage
// id name parent_ref

public class Child {
    private Parent mParent;
    private String mName;

    public Child(String name, Parent parent) {
        this.mName = name;
        this.mParent = parent;
    }

    // loads a child with specific Id
    public static Child load(long id) {
        // query db like 
        Cursor c = db.query("child_table", new String[] {"id", "name", "parent_ref"}, "id = ?", new String[] {String.valueOf(id)});

        // now we have a cursor, we can construct child!
        // Oh my God! I call Parent.load, that calls loadChildren(3)
        return new Child(c.getColumn("name"), Parent.load(c.getColumn("parent_ref")));
    }

    public static Set<Child> loadChildren(Parent parent){ 
        // fetch all child ids, that given parent has
        List<long> childIds = getAllIds(parent);

        // for each id load child (2)
        Child c = Child.load(childId);
    }
}

As you can see, I’d like to load parent by given Id. Parent’s load function call child’s loadlChildren (call (1)), which calls Child.load(call (2)), which calls Parent.load (3) because it needs to make a reference to parent!

So there are two major questions as Im new to java dev.

1) Are there any workarounds about this problems? what Im doing wrong?
2) My call to loadChildren will create n Parents (n references to n objects) instead of creating references to one object. What should I do?

  • 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-14T20:50:12+00:00Added an answer on June 14, 2026 at 8:50 pm

    Why don’t you implement some kind of cache system (maybe with a Java HashMap) where you can put a parent and its children and instead of loading them anyway, load them only if they aren’t already in the cache? And get a reference to them if they are alredy in the cache.

    I’d also have a look at the: Flyweight pattern

    Or another solution I can think is to implement another method in the Parent class: somethink like “loadOnlyParent()” which loads only the Parent (if not already loaded) without the children. And implement a lazy loading of the children (if not already loaded) only when necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about One-To-Many relationship between two tables when there is the
I'm using Delphi XE2 with FireMonkey. I have read many other questions about MD5
There are many questions on StackOverflow about simple, database-less login systems. I was about
I have 3 tables in my SQL Server database: Teacher Student Parent Each of
I have a question about extracting a part of a string. For example I
Questions about Python's subprocess.Popen() objects (Please assume a scenario where the number of bytes
There were many questions about C++ template classes which contain static member variables, as
I'm using a many-to-one relationship as follows: Classes: public class Account { public virtual
There have been a couple of questions about limiting login attempts, but none have
There are many questions on how to find duplicates in a database, but not

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.