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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:22:48+00:00 2026-05-27T22:22:48+00:00

JPA / JPQL: find root elements of a unidirectional tree. I need some help

  • 0

JPA / JPQL: find root elements of a unidirectional tree.

I need some help to write a named query for the following problem.

I have the following (simplified) definition of an entity (TNode) and can save/read it via JPA.
The entity is unidirectional, so childs don’t know the parent!

Now I try to write a named query to get all root elements.

I use JPA 2.0 (Hibernate) and found some hints in the internet, that unidirectional links are supported.

A simple JUnit (4) is also attached, to create two trees and save it into the db. I removed all the asserts.

If I run the Junit, the table looks as expected:

ID  NAME    CHILDS_ID
1   A1  (null)
2   A11 1
3   B1  (null)
4   B11 3

I added @JoinColumn to have the self reference in the same table. And the with simple SQL the problem
would be easy (WHERE childs_id = null). But how must I write this in JPQL?

Thanks for the answers.

Uwe

@Entity
@Table(name = "TNode")
@NamedQueries({ @NamedQuery(name = "getRootNodes", query = "FROM TNode tnode ......."), })
public class TNode implements JPAObject {
    @Id
    @GeneratedValue
    private Integer id;

    private String name;

    @OneToMany(cascade = { CascadeType.REFRESH, CascadeType.MERGE, CascadeType.REMOVE })
    @JoinColumn
    private Set<TNode> childs = new HashSet<TNode>();

    // JPA only
    private TNode() {
    }

    public TNode(String name) {
    this.name = name;
    }

    public void add(TNode tNode) {
    childs.add(tNode);
    }

    @Override
    public Integer getId() {
    return id;
    }

    @Override
    public String getName() {
    return name;
    }
}

Here the simplified Junit

@RunWith(Arquillian.class)
public class TNodeTest {
    @Deployment
    public static Archive<?> createTestArchive() {
    //@formatter:off
    Archive<?> archive = ShrinkWrap.create(WebArchive.class, "test.war")
        .addPackages(true, DummyInterfaceForTest.class.getPackage())
        .addAsLibraries(FindMavenArtifact.find("com.thoughtworks.xstream:xstream:1.4.2"))
        .addAsLibraries(FindMavenArtifact.find("xmlpull:xmlpull:1.1.3.1"))
        .addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml")
        .addAsResource("import.sql", "import.sql")
        .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    //@formatter:on
    System.out.println(archive.toString(true));
    return archive;
    }

    @Inject
    @DAO.DAOType(type = DAO.MODE.STATELESS)
    private DAO<JPAObject> dao;

    @Test
    public void testMutipleRoots() throws Exception {
    TNode root;

    root = new TNode("A1");
    root.add(new TNode("A11"));

    dao.saveOrUpdate(root);

    root = new TNode("B1");
    root.add(new TNode("B11"));

    dao.saveOrUpdate(root);
    }
}
  • 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-27T22:22:49+00:00Added an answer on May 27, 2026 at 10:22 pm

    I found a solution by myself after reading a little bit about JPQL.

    The (Sub-) Query solves the problem:

    query = "SELECT node FROM TNode node WHERE node.id NOT IN (SELECT DISTINCT child.id FROM TNode node, IN(node.childs) child)"), })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to change the value of a column using JPA query syntax(JPQL), how
I need to limit number of entities returned by a query to some certain
I'm new to ORM stuff and I need some help understanding something. Let's assume
I have a problem with JPA 1.0 (OpenJPA) Following situation @Entity public class A{
When I specify columns in my jpql/jpa 2.0 query, i.e. select p.id, p.lastName, p.firstName
I'm trying to evaluate the following JPQL query: select err from Error err where
I'm looking for JPA 2 criteria equivalent of this JPQL query : select e
i´ve got a problem to get jpa running. Following exception appears when i try
With JPA I do not need to code the SQL for every new attribute
I have a some JPA entities that inherit from one another and uses discriminator

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.