-->
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.  [ 3 posts ] 
Author Message
 Post subject: recursive relationships with hibernate
PostPosted: Sun May 20, 2007 7:18 am 
Newbie

Joined: Sat Apr 14, 2007 7:23 am
Posts: 6
Hi,

I'm attempting to set up the storage for a set of heirarchic objects, and have found myself a little confused with the syntax for the hibernate mapping.

Suppose I have an object Node, which can contain multiple child Node objects and also a single parent Node object, plus some other, random properties. How is it best to go about storing this using Hibernate.

----

public class Node
{
private Long id;
private String value;
private Set<Node> children;
private Node parent;
}

----

At the moment, I've been working with a single "nodes" table, and a "parent2child" table as my storage model in mind. The value of the node is trivial, but It's the relationships between the nodes that are confusing me. For the children I have specified:

<set name="children" table="parent2child">
<key column="id"/>
<many-to-many column="id" class="Node" />
</set>

Which looks like it might work, but suggests to me that it might try and give the table two "id" columns.

But for the parent I'm quite stumped. Some sort of use of the <one-to-one> mapping is probably appropriate.

I am very new to Hibernate, so if this is an obvious problem, apologies.

Cheers,

Richard


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 20, 2007 12:25 pm 
Newbie

Joined: Sat Apr 14, 2007 7:23 am
Posts: 6
Hello again,

I've been attacking this problem today, and have now come up against a wall, unfortunately. While running:

session.getTransaction().commit();

I am getting the following error:

org.hibernate.HibernateException: Unable to resolve property: id

I thought think that this is related to the many-to-many mapping that I have been trying (which appeared to be promising) for managing these heirarchic objects:

<id name="id" column="node_id">
<generator class="native" />
</id>

<set name="children" table="parent2child">
<key column="node_id"/>
<many-to-many column="child_id" class="Node" property-ref="id"/>
</set>

So I had hoped that this would allow me to store, in the table parent2child two columns: node_id and child_id (the database is certainly created correctly), and populate both sides with the id property from the object. By changing the name of the id variable to something unique, I have proved that the property "id" to which it refers is the id of the Node class. I have also stepped through my code and watched the save() method save the object to the database, and assign it ids, which appear in the objects themselves.

The only part of the database that is not actually committed after this error occurs is the parent2child table, which remains empty while all other tables are populated.

Any thoughts? Totally stumped now!

Cheers

Richard


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 3:22 pm 
Newbie

Joined: Sat Apr 14, 2007 7:23 am
Posts: 6
Well, it turns out that the id property of an object being persisted with hibernate is not counted as a property of the object /by/ hibernate. So referring explicitly to the id property results in a "no such property" type of error. If you are ever referring to the id property, you should not specify it at all as this appears to be the default.

Thanks to this forum for all the help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.