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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:56:48+00:00 2026-06-01T21:56:48+00:00

I have tree structures that are stored in a DB table. The table can

  • 0

I have tree structures that are stored in a DB table. The table can store multiple trees. I need a query that will return all nodes in a single tree. I’ve used the following sites as resources but the queries in them will load all nodes for all trees into PersistenceContext (isn’t that loading the entire table?). I don’t want to do that, I only want to load one tree. How do I achieve that??

I am using JPA 2 with Hibernate as the provider.

OpenJPA 1.2.x select tree structure using JPQL
http://www.tikalk.com/java/load-a-tree-with-jpa-and-hibernate#comment-1821

[UPDATE] Based upon a suggestion from @bennidi, I wonder if I can use something like this:

@Entity
public class Node {

    private String name;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "rootId")
    private Node root;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "parentId")
    private Node parent;

    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @OrderBy("name")
    private List<Node> children = new LinkedList<Node>();
}

JPQL query:

select distinct n from Node n left join fetch n.children where n.rootId = ROOT_ID

I do have a question though, should I create a second table to maintain the Parent-Child relationships like in this article, or will it be fine if I just used a self-referencing table. Going with the former seems to require a bit more maintenance and probably a little more complex queries. Is it worth it? I’m not sure what problems the article is addressing.

  • 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-01T21:56:49+00:00Added an answer on June 1, 2026 at 9:56 pm

    I think it is not natural to store recursive structures in relational databases. Also the answer to your question would depend on the schema you choose to model your nodes and trees. If you have a self referencing node table that holds the parent child relations as foreign key there is no way to select all nodes that are children of a particular node with one query. I think you should have a table for each tree and have each node reference its tree. Then you can simply select all nodes of a particular tree and created the tree structure by analysing the nodes.

    Have a look at this post: http://www.codeproject.com/Articles/8355/Trees-in-SQL-databases

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

Sidebar

Related Questions

Is there a DB out there that can store tree structures (like for nested
Say instead of documents I have small trees that I need to store in
I have a tree structure that can be n-levels deep, without restriction. That means
I have a table in SQL server that has the normal tree structure of
I have a table in my database where I store a tree structure using
I have a need to store a structure where N parents will have 1
I have a tree data structure that is L levels deep each node has
I have a tree structure in memory that I would like to render in
I have a sort of tree structure that represent a hierarchy of layers in
I have category model that has a tree structure. In my database I have

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.