-->
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: implementing recursive many-to-many relationships
PostPosted: Wed Nov 29, 2006 11:33 am 
Newbie

Joined: Thu Aug 17, 2006 10:18 am
Posts: 9
I have a situation in which I need to have Groups within Groups and its a many-to-many relationship which means the groups can have other groups and the groups could be contained by multiple groups. I've been trying to implement this in Hibernate with no luck. Here is what I have

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 21, 2006 11:48:22 AM by Hibernate Tools 3.2.0.beta6a -->
<hibernate-mapping>
<class name="Group" table="App_Group" schema="dbo" catalog="PI_WEBSERVICE_TEST">
<id name="id" type="string">
<column name="id" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="name" length="50" />
</property>
<property name="desc" type="string">
<column name="description" length="50" />
</property>
<property name="createdBy" type="string">
<column name="createdBy" length="50" />
</property>
<property name="createdDate" type="timestamp">
<column name="createdDate" length="23" />
</property>

<set name="groups" inverse="false"
table="ParentChildGroupRelation"
lazy="false"
cascade="all">
<key column="parentGroupId"/>
<many-to-many class="Group" column="childGroupId"/>
</set>


I have defined a table named ParentChildGroupRelation that has the parentGroupId and childGroupId having a foreign key relationship with the groupId from the Group table. When I try to add a group to an existing group and try to save it no exception is thrown but nothing gets saved in the DB. My cascade options seem to be ok. Did anyone implement this kind of relationship before and if so can you please share your thoughts on this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 11:43 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
I think you need to associations inside your Group POJO: childGroups and parentGroups.

The mapping would be something like:


<class name="Group" table="App_Group" schema="dbo" catalog="PI_WEBSERVICE_TEST">
(...)
<set name="parentGroups" table="ParentChildGroupRelation">
<key column="childgroup_id"/>
<many-to-many column="parentgroup_id" class="Group"/>
</set>
<set name="childGroups" inverse="true" table="ParentChildGroupRelation">
<key column="parentgroup_id"/>
<many-to-many column="childgroup_id" class="Group"/>
</set>
(...)

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 7:37 am 
Beginner
Beginner

Joined: Wed Jan 16, 2008 4:00 am
Posts: 41
Code:
<set name="ParentGroups" table="groups_groups" lazy="false">
         <key column="parentGroupId" />
         <many-to-many column="childGroupId" class="PersistenceLayer.Entities.User.UserGroup, PersistenceLayer" />
      </set>
      <set name="ChildGroups" table="groups_groups" lazy="false">
         <key column="childGroupId" />
         <many-to-many column="parentGroupId" class="PersistenceLayer.Entities.User.UserGroup, PersistenceLayer" />
      </set>


this should work fine. Don't declare an inverse end! By doing so, hibernate will not save the childs / parents


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.