-->
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.  [ 3 posts ] 
Author Message
 Post subject: Delete problem
PostPosted: Sun Dec 16, 2007 2:06 pm 
Newbie

Joined: Sun Mar 18, 2007 12:15 pm
Posts: 19
Hi
My application has 3 items… portraits (ClassPortrait), painters(ClassPainter) and persons (ClassPerson). Each class has an associated table. This setup is used to model a many-to-many relationship. A portrait can have one or more painters.

When NHibernate creates the database, using the mapping files presented here, a constraint is set between painter and person. That is, there must be a person for every painter. (Seems ok to me).

When deleting a person, NHibernate tries to remove the person, without first removing the corresponding painter. This causing SQL Server to throw a constraint error.

Tracing the SQL statements I can see that NHibernate first UPDATES the person to be deleted, and after this NHibernate tries to DELETE the same entry. (Why first an UPDATE?)
I should have expected that NHibernate first deletes the painter entry, then deletes person. The constraints created indicates that NHiberate is avare about the relations in what I am thinking corrrect way (There should not be any painters without a link to a person).

So,,,can anyone help me? What’s wrong in my mapping files?

Code can be seen below.



Hibernate version:
1.2
Code between sessionFactory.openSession() and session.close():
ISession session = factory.OpenSession(interceptor);
ITransaction t = session.Transaction;
t.Begin()
session.Delete(person)
t.Commit() // Error is thrown here



Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="ClassPerson,Entities" table="Person">
      <id name="Id" column="id" unsaved-value="0">
         <generator  class="identity"></generator>
      </id>
      ..
      ..
      ..

      <bag name="Painter" inverse="false" table="Painter" cascade="all-delete-orphan" lazy="false">
         <key column="PersonId"></key>
         <one-to-many class="lgTronic.Spada.Business.Entities.ClassPainter,lgTronic.Spada.Business.Entities"></one-to-many>
      </bag>

   </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="ClassPainter,Entities" table="Painter">
      <id name="Id" column="id" unsaved-value="0">
         <generator  class="identity"></generator>
      </id>
      ..
      ..
      ..
      <many-to-one name="ItemA"   column="PortraitId"  cascade="save-update" lazy="false" class="ClassPortrait,Entities"></many-to-one>
      <many-to-one name="ItemB"  column="PersonId"  cascade="save-update" lazy="false" class="ClassPerson,Entities" >         
         </many-to-one>

   </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="ClassPortrait,Entities" table="Portrait" lazy="false">
   <id name="Id" column="id" unsaved-value="0">
      <generator  class="identity"></generator>
   </id>
   ..
   ..
   ..
   <bag name="Painter"  inverse="false" table="Painter" cascade="all-delete-orphan" lazy="false" >
      <key column="PortraitId"></key>
      <one-to-many class="ClassPainter,Entities"/>
   </bag>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 7:57 am 
Beginner
Beginner

Joined: Tue May 02, 2006 8:04 am
Posts: 34
<bag name="Painter" inverse="true" table="Painter" cascade="all-delete-orphan" lazy="false">
<key column="PersonId"></key>
<one-to-many class="lgTronic.Spada.Business.Entities.ClassPainter,lgTronic.Spada.Business.Entities"></one-to-many>
</bag>

I think that'll solve it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 2:05 pm 
Newbie

Joined: Sun Mar 18, 2007 12:15 pm
Posts: 19
.ben wrote:
<bag name="Painter" inverse="true" table="Painter" cascade="all-delete-orphan" lazy="false">
<key column="PersonId"></key>
<one-to-many class="lgTronic.Spada.Business.Entities.ClassPainter,lgTronic.Spada.Business.Entities"></one-to-many>
</bag>

I think that'll solve it.


That have been my idea also,,, but sorry to say,,, no. It does not solve anything. Still a UPDATE, then a DELETE to the same person record. Painter is untouched.

//lasse


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