-->
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.  [ 4 posts ] 
Author Message
 Post subject: Exception while deleting parent in one-to-many relationship
PostPosted: Mon Oct 17, 2005 8:27 am 
I have two classes with one-to-many relationship: Parent and Child.
Why the following code throws an exception?

Code:
// create a parent with one child
Parent parent = new Parent();
Child child = new Child();
parent.Children.Add( child );
child.Parent = parent;

session = factory.OpenSession();
tx = session.BeginTransaction();
parent = (Parent) session.SaveOrUpdateCopy( parent );
tx.Commit();
session.Close();

// remove child from parent
parent.Children.Remove( child );

session = factory.OpenSession();
tx = session.BeginTransaction();
Parent savedParent = (Parent) session.SaveOrUpdateCopy( parent );
tx.Commit();
session.Close();

session = factory.OpenSession();
tx = session.BeginTransaction();

// --> here is the problem <---
session.Delete( parent );
tx.Commit();
session.Close();


Exception:
Code:
NHibernate.HibernateException: SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count.


hbm
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="test.Parent, test" table="Parent">
    <id name="Id" column="id" type="Int32" unsaved-value="-1">
      <generator class="identity" />
    </id>
    <set name="Children" lazy="false" cascade="all-delete-orphan" inverse="true">
      <key column="parent_id"/>
      <one-to-many class="test.Child, test"/>
    </set>
  </class>
     
  <class name="test.Child, test" table="Child">
    <id name="Id" column="id" type="Int32" unsaved-value="-1">
      <generator class="identity" />
    </id>
    <many-to-one name="Parent" class="test.Parent, test" column="parent_id" not-null="true"/>
  </class>
</hibernate-mapping>


Top
  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 10:29 am 
Nobody an idea?


Top
  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 8:27 pm 
Can you confirm data in the database?


Top
  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 5:52 am 
After the first "SaveOrUpdateCopy(parent)" a parent and a child of it exists in database, after the second call the child was deleted, only the parent exists.


Probably the problem is that I use the parent variable for deleting that I got after the first "SaveOrUpdateCopy". If I use the one I got after the second call of "SaveOrUpdateCopy" it works. Therefore I tried to refresh the parent before I delete it, but it also don't work:
Code:
// don't work
session.Refresh( parent );
session.Delete( parent );

// works
parent = (Parent) session.Get( typeof( Parent), parent.Id );
session.Delete( parent );


Maybe this is an bug in "Session.Refresh" because API says:
"Re-read the state of the given instance from the underlying database."
and I think it should be the same as if I read the object manually from database using "Session.Get".

Did I unterstand this correctly?


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