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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:34:09+00:00 2026-05-20T06:34:09+00:00

Grrrrrr, trying to implement a very simple tree. It doesn’t need to be bidirectional

  • 0

Grrrrrr, trying to implement a very simple tree. It doesn’t need to be bidirectional (since traversals are only top-down), so I assume it would be better (i.e. more space efficient) if it were unidirectional, although my mapping is failing:

@Entity
public class Node extends Model {

    @Column(nullable=false, unique=true)
    public String description;

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    public List<Node> children = new LinkedList<Node>();

    public Node() {}
    public Node(String description) {
        this.description = description;
    }

    public void addChild(String description) {

        Node child = new Node(description);
        child.save();

        this.children.add(child);
    }
}

By “failing”, I mean the child relation doesn’t get mapped, the following unit test fails; the last assert expects 1 but gets 0. In the CRUD section of the app I can see that both nodes are actually created and persisted, but the parent node doesn’t have any children. From this I conclude that the mapping is broken:

public void testMenu() {

    Node root = Node.find("byDescription", "root").first();
    if (root == null) {
        root = new Node("root");
        root.save();
    }

    assertNotNull(root);

    root.addChild("child 1");

    JPA.em().flush();
    JPA.em().getTransaction().commit();
    JPA.em().getTransaction().begin();
    JPA.em().clear(); // clear cache to grab the root from the db

    root = Node.find("byDescription", "root").first();
    assertEquals(1, root.children.size());

}

I’m using Play!, which is why all those members are public and not private, why there are no getters/setters, and why there isn’t an Id field (the Id is inherited from the Model class). It is also why there are some non-standard looking functions there, like the find() and save(). They function very intuitively, and the access to public members is “converted” via reflection to invoke setters and getters that are provided or created.

This doesn’t matter, it is my mapping that is broken so please try and focus on that rather than the side issues, which I’m quite sure aren’t causing the problem.

Cheers.

Update:

The hibernate log:

Hibernate: /* from models.Node where description = ? */ select node0_.id as id35_, node0_.description as descript2_35_ from Node node0_ where node0_.description=? limit ?
Hibernate: /* insert models.Node */ insert into Node (description) values (?)
Hibernate: /* insert models.Node */ insert into Node (description) values (?)
Hibernate: /* from models.Node where description = ? */ select node0_.id as id35_, node0_.description as descript2_35_ from Node node0_ where node0_.description=? limit ?
Hibernate: /* load collection models.Node.children */ select children0_.Node_id as Node1_35_1_, children0_.children_id as children2_1_, node1_.id as id35_0_, node1_.description as descript2_35_0_ from Node_Node children0_ inner join Node node1_ on children0_.children_id=node1_.id where children0_.Node_id=?
  • 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-20T06:34:10+00:00Added an answer on May 20, 2026 at 6:34 am

    It would help if you pasted the hibernate log.

    It shouldn’t be strictly necessary but you could try root.save() after root.addChild(“child 1”);

    In addition you didn’t commit the transaction, that wouldn’t be a problem if the Node.find() uses the same session, but does it?

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

Sidebar

Related Questions

No related questions found

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.