-->
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.  [ 8 posts ] 
Author Message
 Post subject: Delete / cascade / many to many
PostPosted: Tue Aug 15, 2006 12:39 pm 
Newbie

Joined: Fri May 05, 2006 4:54 am
Posts: 14
Hello!

I use NHibernate 1.02
I´ve been struggling with this in a couple of days now. But it´s surely a simple question to answer..) I got two tables contact and group, a many to many connection. And a table between named groupcontact.
When I add contacts to a group everything works fine, but how can i delete a contact from the contact table and get the groupcontact table updated with this deletion? I´ve tried with cascade = "delete" but this results in deletion of all contacts and all groups that that contact was tied to. I got inverse = "true" on the group side.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 6:38 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you have inverse="true" on the group side, that implies that inverse="false" is on the contact side. So cascade="delete" from contact will delete groups. If you don't want this behvaiour, your options are
  1. to change the direction of the association, and put the inverse="true" on the contact side,
  2. or to turn off cascade="delete".
Rows in the join table are always deleted when you delete corresponding rows from either "main" table. You don't need any special settings for inverse or cascade for this. The inverse and cascade settings affect the "main" tables on the far side of the join table.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 1:28 pm 
Newbie

Joined: Fri May 05, 2006 4:54 am
Posts: 14
Thanks for the answer! But still I cant´t get it to work..
With the code below, only the contact is deleted from the main table.

Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="Business" assembly="Business">
  <class name="Business.Contact, Business" table="Contact" >
    <id name="Id" column ="id" type="System.Int32" >
      <generator class="native" />
    </id>
    <property name="FirstName" />
    <property name="LastName"/>
    <property name="EMail"/>
    <property name="TelePhone"/>
    <property name="UserName"/>
    <property name="Picture" type="BinaryBlob"/>
    <bag name="Groups" table="GroupContact" inverse="true" lazy="true">
      <key column="contactid" />
      <many-to-many class="Business.Groups, Business" column="groupid" />
    </bag>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="Business" assembly="Business">
  <class name="Business.Groups" table="Groups">
    <id name="GroupId" column="groupId" type="System.Int32">
      <generator class="native" />
    </id>
    <property name="GroupName" type="String" />
    <property name="UserName" type="String" />
    <bag name="Contacts" table="GroupContact" cascade="delete" lazy="true">
      <key column="groupid" />
      <many-to-many class="Business.Contact, Business" column="contactid" />
    </bag>
  </class>
</hibernate-mapping>

In code I do this:

Contact p = (Contact)session.Load(typeof(Contact), Convert.ToInt32(contacts[i].ToString()));
                    session.Delete(p);
                    session.Flush();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 5:30 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That code is deleting contact p but leaving references to it in the GroupContact table? That doesn't happen in my code, which admittedly is hibernate3.

I would expect your current mapping to delete any GroupContact rows when the relevant Contact is deleted, and to delete any Contacts and GroupContact rows when a Group is deleted.

Try changing the bags to sets. I've never used bags in production code because of their inefficiencies (inserting or deleting items is very expensive for bags), so it's possible that what you're seeing is correct behaviour for bags. Unlikely, though...

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 7:28 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
I think tenwit got it backwards ,
make
inverse="true" and cascade="none"
and
inverse ="false"

on groups. That should get it to work

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 2:35 pm 
Newbie

Joined: Fri May 05, 2006 4:54 am
Posts: 14
Yes! It worked the way scareface said. Thank you both!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 22, 2006 5:45 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Not backwards, upside down! It's what we do, here on the underside of the world.

That's my excuse and I'm sticking to it ;)

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


Top
 Profile  
 
 Post subject: Same prblm
PostPosted: Wed Sep 17, 2008 1:00 pm 
Beginner
Beginner

Joined: Fri May 11, 2007 11:03 am
Posts: 32
I was trying the same thing.But deleting the relationship works fine if have cascade =none and inverse=true on one side but the addition of one object to the other stops working . For addition I have to set it to "all-delete-orphan"
I am doing something wrong?


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