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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:22:22+00:00 2026-05-20T05:22:22+00:00

My first project with Hibernate/JPA and Play!, I’ve got a menu that once working,

  • 0

My first project with Hibernate/JPA and Play!, I’ve got a menu that once working, will support changes (i.e. easily add nodes to the tree). Struggling (in the >5 hours sense) just to get the basic modelling together:

The model:

@Entity
@Table(name = "Node")
public class Node extends Model {

    @Column(nullable=false)
    public String description;

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

    @ManyToOne
    @JoinColumn(name="parent", insertable=false, updatable=false)
    public Node parent;

    public Node(){}

}

The util class:

public class NodeUtil {

    public static void addChild(Node root, String description) {

        Node child = new Node();
        child.description = description;
        child.parent = root;

        root.children.add(child);

        root.save();

    }

    private static final String MENU_NAME = "MainMenu";

    public static Node getMenu() {
        return getRoot(MENU_NAME);
    }
    public static Node getRoot(String name) {
        Node root = Node.find("byDescription", name).first();
        if (root == null) {
            root = new Node();
            root.description = name;
            root.save();
        }

        return root;
    }

}

The test:

public class MenuTest extends UnitTest {

    private static final String TEST_MENU = "testMenu";

    @Test
    public void testMenu() {

        // test build/get

        Node root = NodeUtil.getRoot(TEST_MENU);
        assertNotNull(root);

        // delete all children - maybe from previous tests etc.
        for(Node o : root.children)
           o.delete();
        root.save();

        // test add

        root = NodeUtil.getRoot(TEST_MENU);

        NodeUtil.addChild(root, "child 1");
        NodeUtil.addChild(root, "child 2");
        NodeUtil.addChild(root, "child 3");

        assertEquals(3, root.children.size());
        assertEquals("child 3", root.children.get(2).description);

        assertEquals(0, root.children.get(1).children.size());

        Node node = root.children.get(1);
        NodeUtil.addChild(node, "subchild 1");
        NodeUtil.addChild(node, "subchild 2");
        NodeUtil.addChild(node, "subchild 3");
        NodeUtil.addChild(node, "subchild 4");

        NodeUtil.addChild(root.children.get(2), "sub subchild");

        assertEquals("sub subchild", root
                                    .children.get(1)
                                    .children.get(2)
                                    .children.get(0)
                                    .description);

        assertEquals(4, root.children.get(1).children.size());

        root.save();

        // test delete

        root = NodeUtil.getRoot(TEST_MENU); // regrab the root via hibernate, assuming there isnt it isnt cached this will get changes that have been persisted to the db (maybe?)

        root.children.get(1).children.get(2).delete();
        assertEquals(3, root.children.get(1).children.size());

        //root.delete();

    }

}

Questions:

  1. What am I doing wrong? (I.e. I just can’t get this simple idea to be modelled and to pass the unit test. Like I said, new to Hibernate, and every change I make yields a new Hibernate error variant, which means nothing to me. E.g. this current setup throws “detached entity passed to persist: models.Node”)

  2. Initially I had the entire util class as a bunch of static methods in the model class. Firstly, do static methods affect Hibernates modelling? If so, in brief, under what circumstances can I have static methods (and members, come to think of it) that will be “transient” to the object modelling?

  3. Assuming that I keep the util methods in a separate public util class, where is this class conventionally stored in the play framework? At the moment it’s in the models package, next to the Node.java.

  • 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-20T05:22:23+00:00Added an answer on May 20, 2026 at 5:22 am

    I’m not familiar with Play Framework, but I can make some point regarding working with Hibernate:

    • Maintaining consistent state of the objects in memory is your responsibility. Consider the following code:

      for(Node o : root.children)
          o.delete();
      root.save();
      

      You instructed Hibernate to delete children from the database, but the root object in memory still references them. Since the relationship is configured with cascading, Hibernate will try to save them again (I guess it’s the reason of “detached entity passed to persist” error). So, keep in-memory state of the object consistent by clearing root.children.

    • Hibernate heavily relies on the concept of Unit of Work. I’m not sure how Play manages it, but it looks like you should call clearJPASession() in unittests to make sure that exisiting session state wouldn’t affect further operations:

      root.save();
      
      clearJPASession();
      
      // test delete
      root = NodeUtil.getRoot(TEST_MENU);
      
    • The way you defined a relationship is supported, but not recommended (see 2.2.5.3.1.1. Bidirectional). Use the following approach instead:

       @OneToMany(mappedBy = "parent", cascade=CascadeType.ALL, fetch=FetchType.EAGER)                           
       public List<Node> children = new LinkedList<Node>();
      
       @ManyToOne
       @JoinColumn(name="parent")
       public Node parent;
      
    • Static methods doesn’t interfere with Hibernate.

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

Sidebar

Related Questions

I'm working on a maven project which uses seam 2.2.0, hibernate 3.5.0-CR-2 as JPA
i'm working on a project in which i use JPA, Hibernate and all this
I'm working with JPA(Hibernate) and spring project these days, I configured spring, jpa and
I'm developing a web application using Wicket+Spring+JPA+Hibernate. This is my first project with this
I'm writing my first Hibernate JPA project. I have a very common issue (I
I have been moving a project from JPA to Hibernate native implementation (got outvoted
I've added my first OneToMany relationship to my hibernate 3.6.10 project. Here is the
I'm working on my first project in which I'm utilizing PHPUnit for unit testing.
im using drupal 6.15 and doing my first project in drupal . i got
I would like to implement pagination in my Servlet/EJB/JPA-Hibernate project, but I can't figure

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.