-->
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.  [ 13 posts ] 
Author Message
 Post subject: Parent / Child / Delete
PostPosted: Thu May 26, 2005 1:51 pm 
Beginner
Beginner

Joined: Tue Dec 21, 2004 5:34 pm
Posts: 25
I feel rather dumb asking this question. I've read the docs but I'm sure I'm missing something.

I'm having issues deleting a parent object and having its children deleted as well. I have two basic classes: User and Item. A User can have 1..m Items. Below are snippets of my mappings.

Code:
    <set name="Items" inverse="true" cascade="all-delete-orphan" lazy="false">
      <key column="UserId" />
      <one-to-many class="NHibernateTest.Item, NHibernateTest" />
    </set>


Code:
<many-to-one name="User" class="NHibernateTest.User, NHibernateTest" column="UserId" not-null="true" />


I have two tables. A USR table and an ITEM table. The ITEM table has a foreign key contraint on the USR table, UserId. A simple 1..m relationship.


Below is the code I am using to delete the User. I want the items to be deleted as well.

Code:
    public void Delete(User user) {
      ISession session = factory.OpenSession();
      ITransaction tx = null;

      try {
        tx = session.BeginTransaction();

        session.Delete(user);

        tx.Commit();
      } catch (HibernateException e) {
        if (tx != null) {
          tx.Rollback();
        }
        throw e;
      } finally {
        session.Close();
      }
    }


In short this is not working for me. When I turn logging on I notice that its trying to delete the User object before the Items.

I'm using MS SQL and NHibernate 0.8.3.0

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 3:44 pm 
Beginner
Beginner

Joined: Tue Dec 21, 2004 5:34 pm
Posts: 25
Any thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 5:45 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
inverse="true" tells NH which end of the association to use when synchronizing the DB. in this case, you are telling NH to use the "Item" side of the collection (or the side of the collection that resides in the "Item" class).

Two optins: set inverse="true" on the other side, or manually delete the Items collection from the User. imho, you want the set the inverse="true" on the other side of the collection.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 10:13 am 
Beginner
Beginner

Joined: Tue Dec 21, 2004 5:34 pm
Posts: 25
I don't believe that the many-to-one attribute supports the inverse attribute. The only place I can set the inverse attribute is on the collection side: set, bag, etc.

I'm pretty sure you don't have to delete each item from the user to get this done. You should just be able to delete the User and all the Items should be deleted as well.

I feel like I've read the docs correctly but fear I've missed something simple to make this work. Could it be that I'm trying to do this in a transaction? Is there a method I need to call after Delete?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 10:52 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
umm... nothing will actually get "finalized" in the database until you do a Session.Flush();

This will be done behind-the-scenes by NHibernate, but you should typically do it after any save/update/delete.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 1:07 pm 
Beginner
Beginner

Joined: Tue Dec 21, 2004 5:34 pm
Posts: 25
I tried a session.Flush() after my session.Delete(user). No go, it still tries to delete the user before the items.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 4:20 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Does it work when you set inverse="false"? If it does, it means delete-orphans interacts badly with inverse="true" and we'll have to look into it some time, but maybe the problem is somewhere else actually.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 04, 2005 9:17 am 
Beginner
Beginner

Joined: Tue Dec 21, 2004 5:34 pm
Posts: 25
That didn't work either. Thanks everyone, I do appreciate the help.

Is there a working example? One that has a foreign key contrant on the parent table? Or can someone show that in a relationship like this that the items are deleted first shown in a log?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 11:04 am 
Beginner
Beginner

Joined: Tue Dec 21, 2004 5:34 pm
Posts: 25
Does anyone have a working example or can confirm this is a bug?


Top
 Profile  
 
 Post subject: Re: Parent / Child / Delete
PostPosted: Fri Jun 10, 2005 12:30 pm 
Regular
Regular

Joined: Mon May 16, 2005 2:15 pm
Posts: 59
wmopnc wrote:
In short this is not working for me. When I turn logging on I notice that its trying to delete the User object before the Items.


Hmm... I have a very simple Parent / Child sample on my system that is mapped very much like you show here, and it deletes the children before the parents.

When you created the Items classes, did you also add them to the Items collection of the parent in addition to setting the User property of the of the item... Something like:

User u = new User();

Item i = new Item();

i.User = u;
u.Items.Add(i);

Sesssion.Save(u);

*******

BOb


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 2:38 pm 
Beginner
Beginner

Joined: Tue Dec 21, 2004 5:34 pm
Posts: 25
I'm actually looking up the data. So all the data is in the table. I just look for the User by my primary key and go from there. The items are in my collection property for my users, Items. So all seems to be right there.

So in you log output, do you see it deleting you children objects first, then your parent object?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 11:26 pm 
Regular
Regular

Joined: Mon May 16, 2005 2:15 pm
Posts: 59
wmopnc wrote:
So in you log output, do you see it deleting you children objects first, then your parent object?


Yes... here is the applicable part of the log:

2005-06-10 23:21:35,045 [TestCasemanager::ExecutionThread] INFO NHibernate.Impl.BatcherImpl [] [] - Preparing DELETE FROM child WHERE cid = @p0
2005-06-10 23:21:35,225 [TestCasemanager::ExecutionThread] INFO NHibernate.Impl.BatcherImpl [] [] - Preparing DELETE FROM child WHERE cid = @p0
2005-06-10 23:21:35,235 [TestCasemanager::ExecutionThread] INFO NHibernate.Impl.BatcherImpl [] [] - Preparing DELETE FROM child WHERE cid = @p0
2005-06-10 23:21:35,235 [TestCasemanager::ExecutionThread] INFO NHibernate.Impl.BatcherImpl [] [] - Preparing DELETE FROM parent WHERE pid = @p0

Here is the code I am using:

Code:
Parent p2 = (Parent)session.Load(typeof(Parent), savedId);
transaction = session.BeginTransaction();
session.Delete(p2);

transaction.Commit();
session.Close();


Of course, it seems to me it would be more efficient if NHibernate sent a single delete to delete the children where the FK equaled the PK of the parent.

HTH,
BOb


Top
 Profile  
 
 Post subject: Similar Problem
PostPosted: Wed Aug 10, 2005 12:00 pm 
I am having a similar problem.

I have a child parent relationship, and a FK in the database.
My xml mapping files are essentially the same.

In the parent xml file I have.

Code:
<bag name="Child" lazy="true" cascade="all-delete-orphan">
   <key column="parentid"/>         
   <one-to-many class="RightRez.DBLibrary.dbo.Child, DBLibrary"/>
</bag>


In the child I have:

Code:
<many-to-one name="Parent" column="parentid" class="RightRez.DBLibrary.dbo.Parent, DBLibrary" not-null="true"/>


When I delete, I have tried to do a session.Delete(c) for all children of hte parent, and then session.Delete(p).

I have also tried just a straight session.Delete(p)

Both times I get a FK constraint error. When looking at the log, it shows that it does an update (before any deletes) of a child, and sets the parentid of the child to null??? I am NOT doing a Remove call. So what the heck is going on. I have played around with many different cascade rules, and have tried turning inverse off and on.

Any help would be appreciated!


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