-->
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: Many-to-Many, can both sides manage the relationship?
PostPosted: Sat Nov 27, 2004 2:57 pm 
Newbie

Joined: Sun Nov 07, 2004 3:38 am
Posts: 3
Section 6.8 of the user manual states that a bidirectional relationship is maintained by one end of the relationship. I have a situation where I really want both sides to manage the relationship.

This has to do with Users and UserGroups: One User can belong to many UserGroups, and a UserGroup can belong to many users. Everything works fine, except when I want to remove a UserGroup. The desired behavior is that when I remove a User, I want User-UserGroup relationship to be removed, and when I remove a UserGroup, I also want the that User-UserGroup relationship removed. The problem is, when I attempt to remove a UserGroup that's assigned to a user, I get an exception and the UserGroup is not removed (neither is the relation to User(s)). Here are my mapping files:

Code:
<hibernate-mapping>
  <class name="SuiteUser" table="SuiteUser">
  <id name="ID" type="long" column="ID" unsaved-value="null" >
     <generator class="native"/>
   </id>
      
    <property name="userName" column="userName" type="string"/>
   
    <set name="userGroups" table="group_membership" cascade="all">
      <key>
            <column name="user_id" not-null="true"/>
      </key>
      <many-to-many class="UserGroup">
                <column name="group_id" not-null="true" />
      </many-to-many>
      </set>

   </class>
</hibernate-mapping>


Code:

<hibernate-mapping>
   <class name="UserGroup" table="UserGroup">
    <id name="id" type="long" column="id" unsaved-value="null" >
      <generator class="native"/>
    </id>

    <property name="description" column="description" type="string"/>
    <property name="groupName" column="groupName" type="string" not-null="true" />

    <set name="users" table="group_membership" inverse="true" lazy="true">
     <key>
         <column name="group_id" not-null="true"/>
     </key>
     <many-to-many class="SuiteUser">
           <column name="user_id" not-null="true"/>
     </many-to-many>
</set>

   </class>
</hibernate-mapping>


Thanks for any suggestions,

Frank


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 20, 2004 2:33 am 
Newbie

Joined: Tue Dec 14, 2004 11:56 am
Posts: 1
perhaps ,first delete the relation object 'group_membership'

session.delete("from usergroup ug where ug.groupid= ?",group.getGroupid(),Hibernate.LONG);


second delete the object 'group'

session.delete(group);


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.