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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:28:29+00:00 2026-05-16T15:28:29+00:00

Does JPA 2 have any mechanism for running recursive queries? Here’s my situation: I

  • 0

Does JPA 2 have any mechanism for running recursive queries?

Here’s my situation: I have an entity E, which contains an integer field x. It also may have children of type E, mapped via @OneToMany. What I’d like to do is find an E by primary key, and get its value of x, along with the x values of all its descendants. Is there any way to do this in a single query?

I’m using Hibernate 3.5.3, but I’d prefer not to have any explicit dependencies on Hibernate APIs.


EDIT: According to this item, Hibernate does not have this feature, or at least it didn’t in March. So it seems unlikely that JPA would have it, but I’d like to make sure.

  • 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-05-16T15:28:30+00:00Added an answer on May 16, 2026 at 3:28 pm

    Using the simple Adjacency Model where each row contains a reference to its parents which will refer to another row in same table doesn’t co-operate well with JPA. This is because JPA doesn’t have support for generating queries using the Oracle CONNECT BY clause or the SQL standard WITH statement. Without either of those 2 clauses its not really possible to make the Adjacency Model useful.

    However, there are a couple of other approaches to modelling this problem that can applied to this problem. The first is the Materialised Path Model. This is where the full path to the node is flattened into a single column. The table definition is extended like so:

    CREATE TABLE node (id INTEGER,
                       path VARCHAR, 
                       parent_id INTEGER REFERENCES node(id));
    

    To insert a tree of nodes looks some thing like:

    INSERT INTO node VALUES (1, '1', NULL);  -- Root Node
    INSERT INTO node VALUES (2, '1.2', 1);   -- 1st Child of '1'
    INSERT INTO node VALUES (3, '1.3', 1);   -- 2nd Child of '1'
    INSERT INTO node VALUES (4, '1.3.4', 3); -- Child of '3'
    

    So to get Node ‘1’ and all of its children the query is:

    SELECT * FROM node WHERE id = 1 OR path LIKE '1.%';
    

    To map this to JPA just make the ‘path’ column an attribute of your persistent object. You will however have to do the book-keeping to keep the ‘path’ field up to date. JPA/Hibernate won’t do this for you. E.g. if you move the node to a different parent you will have to update both the parent reference and determine the new path value from the new parent object.

    The other approach is called the Nested Set Model, which is bit more complex. Probably best described by its originator (rather than added verbatim by me).

    There is a third approach called Nested Interval Model, however this has a heavy reliance of stored procedures to implement.

    A much more complete explanation to this problem is described in chapter 7 of The Art of SQL.

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

Sidebar

Related Questions

I'm using JPA 2.0, more precisely Eclipselink. Here's my problem: I have an entity
When merging JPA entities using JpaTemplate in methods marked with @Async, the entity does
Does COUNT(*) have any significant impact for MySQL performance if query already has GROUP
I have been using Hibernate ORM, which is an implementation of the JPA specification
Does OpenJPA have any support for batch insert similar to Hibernate ? I haven't
Does anyone have any ideas why a test would pass when run with ant,
In a JPA app I have a scenario in which the app is to
Java has the transient keyword. Why does JPA have @Transient instead of simply using
I have an JPA entity like this: @Entity @Table(name = category) public class Category
I have problem in a JPA entity class. This entity was working perfectly as

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.