-->
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.  [ 5 posts ] 
Author Message
 Post subject: bidirectional many-to-many tree (parent, child = same class)
PostPosted: Mon May 29, 2006 8:34 am 
Newbie

Joined: Mon May 29, 2006 7:52 am
Posts: 3
Hi,

I needed to create mapping for graph structure. It looks like normal tree with some exception. Every node has 0-N children and every node has 0-N parents (instead of only one). Node's children should be ordered, parents not.

I can do mapping for normal tree but I'm totally lost with this.

I read reference documentation and HIA, but can't make it work.

The "most" working solution, with which I've ended is attached at the end of this post. It makes at leat correct tables and when building the tree, it fills something inside. It's not throwing any exception. But when loading the data back, the structure is not correct. It seems all nodes are present in two copies. All examples I found were describing normal tree structure or many-to-many association with two different classes. But I need many-to-many, bidirectional, ordered (from one side) on the same class.

I would be glad for any hint or help.

<hibernate-mapping>
<class name="PageData" table="PageData">
<id name="id" column="pagedata_id" type="long">
<generator class="native"/>
</id>

<property name="name" column="name" type="string"/>
<list
name="children"
table="children_parent"
cascade="save-update">

<key column="child_pagedata_id" />
<index column="position" />
<many-to-many class="PageData" column="pagedata_id"/>
</list>

<set
name="parents"
inverse="true"
table="children_parent">

<key column="child_pagedata_id" />
<many-to-many class="PageData" column="pagedata_id"/>
</set>
</class>
</hibernate-mapping>

-----------------------------------

public class PageData implements Serializable {
private Long id;
private String name;

private List<PageData> children = new ArrayList<PageData>();
private Set<PageData> parents = new HashSet<PageData>();
...
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 29, 2006 9:51 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Your problem is that you've mapped your parent and child relationships identically (apart from using list on one side, of course). That is, both relationships have key column="child_pagedata_id" and both have many-to-many column="pagedata_id".

One of the collections shoudl have key column="pagedata_id" and many-to-many column="child_pagedata_id". You need to swap the key column and many-to-many colulmn in either Children or Parents (but not both). You should be able to figure out which is which through experimentation.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 7:00 am 
Newbie

Joined: Mon May 29, 2006 7:52 am
Posts: 3
Thanks for your reply.

I'll try your advice. But I need one minor specifiacation. What do you mean with "apart from using list on one side, of course". Should I set up both sides as "list"? Or am I not allowed to use lists in this context at all and I have to use 'set' on both sides? How would I keep the order of the node children then?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 5:41 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
No, you can continue to use list on either (or both) sides. I meant that the only difference between your parents and children collection is that one is a list and one is a set. But they're both children collections (or both parent collections), due to the fact that they both have the same key/column pairs. They need to have those two columns mapped "oppositely".

Ooo, I made up a word!

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 4:39 am 
Newbie

Joined: Mon May 29, 2006 7:52 am
Posts: 3
ok, sorry for misunderstanding...

Thanks for a great and fast support of beginners!


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