-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Simple self-referencing
PostPosted: Mon Oct 01, 2007 12:27 pm 
Newbie

Joined: Mon Oct 01, 2007 12:09 pm
Posts: 2
OK, Hibernate newbie here. This should probably be really easy, if I just knew how.

I'm modelling a hierarchy (or tree structure, if you like). Each Node has a parent Node, and a Node can have any number of child Nodes.

The database table should look like this:
Code:
ID | PARENT_ID | ...


I've written this Node.hbm.xml:
Code:
<hibernate-mapping>
    <class name="Node" table="NODE">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <many-to-one name="parent" class="Node" column="PARENT_ID"/>
        <set name="children" inverse="true" cascade="all">
            <key column="PARENT_ID" not-null="true"/>
            <one-to-many class="Node"/>
        </set>
    </class>
</hibernate-mapping>


... and Node.java:

Code:
public class Node {

    private long id;
    private Node parent;
    private Collection<Node> children = new HashSet<Node>();

    public Collection<Node> getChildren() {
        return children;
    }

    public void setChildren(Collection<Node> children) {
        this.children = children;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }
}


Then, I run this testcase (in pseudo-code):
    // Create a parentNode and save it
    // Create a childNode, set childNode.parent to parentNode and save it
    // Retrieve childNode from DB
    // Inspect childNode.parentNode, assert that it equals parentNode
    // Retrieve parentNode from DB
    // Inspect parentNode.children, assert it has size == 1

This is where the testcase fails, as parentNode.children is always empty (size == 0).

What am I doing wrong here? Any suggestions appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 8:30 pm 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
If you do all of this in one session:

// Create a parentNode and save it
// Create a childNode, set childNode.parent to parentNode and save it
// Retrieve childNode from DB
// Inspect childNode.parentNode, assert that it equals parentNode
// Retrieve parentNode from DB
// Inspect parentNode.children, assert it has size == 1

I assume, that the parrentNode is loaded from session-level cache, and so there the collection is not updated.

Regards,
Pavol


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.