-->
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: Many to many relationships and deletions
PostPosted: Tue Mar 13, 2007 6:03 pm 
Newbie

Joined: Tue Jan 11, 2005 6:38 pm
Posts: 5
I have a many to many relationship that seems to work ok, except when I try and remove a User object. When I remove a User object by calling delete (using the Spring HibernateTemplate object) it removes not only the User but all the groups assigned to that user and all the users assigned to those groups.

What I want is if I delete a User it should delete the user and any associations to groups but the groups and other users should remain. I'm really new to Hibernate so I'm sure I'm just missing something simple but it's been driving me crazy.

The mapping is below:

User:
<class name="Users" table="USERS">
<meta attribute="sync-DAO">false</meta>
<id name="Id" type="java.lang.Long" column="USERID">
<generator class="identity"/>
</id>
<property name="Username" column="USERNAME" type="string" not-null="true" length="128" />
<property name="Password" column="PASSWORD" type="string" not-null="true" length="32" />
<property name="Firstname" column="FIRSTNAME" type="string" not-null="false" length="128" />
<property name="Lastname" column="LASTNAME" type="string" not-null="false" length="128" />
<property name="Customerid" column="CUSTOMERID" type="java.lang.Long" not-null="true" length="19" />
<property name="Shipid" column="SHIPID" type="java.lang.Long" not-null="true" length="19" />
<property name="Canadian" column="CANADIAN" type="string" not-null="false" length="1" />
<property name="Email" column="EMAIL" type="string" not-null="false" length="128" />
<!-- inverse end -->
<set name="groups" table="USERSGROUPS" lazy="false" cascade="save-update">
<key column="USERID"/>
<many-to-many class="com.coastdist.domain.Groups" column="GROUPID"/>
</set>
<!--
<set name="shipids" table="SHIPIDS">
<key column="USERID"/>
<element type="string" column="SHIPID"/>
</set>
-->
</class>

Group:
<class name="Groups" table="GROUPS" mutable="false">
<meta attribute="sync-DAO">false</meta>
<id name="Id" type="java.lang.Long" column="GROUPID">
<generator class="identity"/>
</id>
<property name="Groupname" column="GROUPNAME" type="string" not-null="true" length="128" />
<property name="Description" column="DESCRIPTION" type="string" not-null="false" length="254" />
<set name="users" table="USERSGROUPS" lazy="false" inverse="true" cascade="all">
<key column="GROUPID"/>
<many-to-many class="com.coastdist.domain.Users" column="USERID"/>
</set>
</class>


Top
 Profile  
 
 Post subject: Quick note...
PostPosted: Tue Mar 13, 2007 8:59 pm 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
Not sure if this is related but do you need that 'inverse="true"' in your group to users mapping? I thought that was used for one-to-many relationships.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 9:54 pm 
Newbie

Joined: Tue Jan 11, 2005 6:38 pm
Posts: 5
I believe it's needed to determine which side of the mapping is the authoritative, although I'm going off of documentation and the Hibernate book. I have not fully tested it. I'm so unfamiliar with many of the different options I'm not sure if it's truly needed.

That being said, I think I found my problem. It turns out I misunderstood the cascade options. Turns out I only need 'save-update' instead of all. Changing it to save-update fixed the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 14, 2007 2:35 am 
Newbie

Joined: Wed Mar 07, 2007 3:26 am
Posts: 16
Location: Hyderabad,India
hi, i have also faced same problem iam giving u 2 suggestions not a solution because iam also not sure ok

1)<set name="users" table="USERSGROUPS" lazy="false" inverse="true" cascade="all">

delete cacade="all" attribute in ur set tag ok i think this will work

y because cascade="all" means it deletes/updates/saves on every operation of parent including all of its child objects so any where cascade="all" is there just delete it or make it cascade="save-update"

2)in ur data base go to fields of that table and make it set no action for ondelete cascade option

_________________
sam


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 14, 2007 2:36 am 
Newbie

Joined: Wed Mar 07, 2007 3:26 am
Posts: 16
Location: Hyderabad,India
hi, i have also faced same problem iam giving u 2 suggestions not a solution because iam also not sure ok

1)<set name="users" table="USERSGROUPS" lazy="false" inverse="true" cascade="all">

delete cacade="all" attribute in ur set tag ok i think this will work

y because cascade="all" means it deletes/updates/saves on every operation of parent including all of its child objects so any where cascade="all" is there just delete it or make it cascade="save-update"

2)in ur data base go to fields of that table and make it set no action for ondelete cascade option

_________________
sam


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.