-->
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: update and delete instead of just delete?
PostPosted: Tue Dec 04, 2007 1:31 pm 
Newbie

Joined: Mon Nov 19, 2007 6:43 am
Posts: 6
Hy,

I have classical one-to-many relationship (parent - childs). In DB child entity has foreign key on parent.
I read parent with lazy="true", so I get all childs automatically. I remove one child from parent.Child collection (I use Set) and when I try to save parent object I see in SQL profiler that NH generated two statements 1st: update child row.FK_field = null and 2nd delete statement?

How can I get just delete statement. Because now (as described above) I must allow nulls on child.FK_field in DB and for my model that is not OK.


tenx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 1:58 pm 
Newbie

Joined: Mon Dec 03, 2007 10:50 pm
Posts: 6
Do you have a not-null="true" attribute on the parent property of the child object?

--randy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 6:15 pm 
Newbie

Joined: Mon Nov 19, 2007 6:43 am
Posts: 6
Yes,

tag for that fk field on child entity is like this:

<many-to-one name="parent" column="PARENT_ID" not-null="true" outer-join="auto"></many-to-one>

tenx ...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 6:45 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We have the same issue. Since our entities implement a specific interface that tracks dirty and deleted status, we were able to do this in IInterceptor.FindDirty:

Code:
public int[] FindDirty(
   object   entity,
   object   id,
   object[] currentState,
   object[] previousState,
   string[] propertyNames,
   NHibernate.Type.IType[] types)
{
   OurCompany.BLL.IEntity ent = entity as OurCompany.BLL.IEntity;

   if (!this.IsChanged(ent) || this.IsDeleted(ent))
   {
      // Don't try to update entities marked deleted,
      // even if they appear dirty via the default algorithm.
      // Seems like an NHibernate bug that it would consider it for update ...

      return new int[0];
   }

   return null;
}


However, assuming you haven't gone to the same trouble to track those status values, I don't know how else it can be dealt with. I'd submit a JIRA issue ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 10:00 am 
Newbie

Joined: Mon Nov 19, 2007 6:43 am
Posts: 6
YoU assumed right :-) what is JIRA


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 12:35 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
JIRA is the issue tracking system that NHibernate uses. Here is the link:
http://jira.nhibernate.org/secure/Dashboard.jspa


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 3:36 pm 
Newbie

Joined: Tue Oct 11, 2005 8:02 am
Posts: 6
Mare,

I've just made a small example with two tables - Orders and OrderDetails. Orders has a Set of OrderDetails, and OrderDetails has a many-to-one element associated with Orders table. All fields don't allow nulls.

Everything works OK - I load Orders entity, remove one OrderDetails element from Orders' Set and NHibernate produces only one Delete Sql command. The OrderDetails Set defined in HBM for Orders entity has cascade="all-delete-orphan" so maybe check the value in the cascade tag.

If this wouldn't help please post both HBM files. Here are HBM files for the example described above:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="OrdersEntity, Aaa.Server.Data" table="Orders">
  <id name="Id" column="IdOrder" type="System.Int32"unsaved-value="null"><generator class="native"/></id>
  <property name="Name"  column="Name" type="System.String" not-null="true" length="50"/>
  <set name="OrderDetailsSet"
    cascade="all-delete-orphan"
    inverse="true"
    lazy="true">
    <key column="IdOrder"/>
    <one-to-many class="OrderDetailsEntity, Aaa.Server.Data"/>
  </set>
</class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="OrderDetailsEntity, Aaa.Server.Data" table="OrderDetails">
  <id name="Id" column="IdOrderDetail" type="System.Int32" unsaved-value="null"><generator class="native"/></id>
  <property name="Amount"  column="Amount" type="System.Double" not-null="true" />                               
  <many-to-one name="Order" column="IdOrder" not-null="true" outer-join="auto"></many-to-one>
</class>
</hibernate-mapping>

_________________
Best regards,
Thomas

--------------------------------------------------------
NHibernate based .NET application generator
http://www.nconstruct.com
--------------------------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 10:32 am 
Newbie

Joined: Mon Nov 19, 2007 6:43 am
Posts: 6
Tomas,

Tenx for answer. I found the reason: inside set tag I was missing inverse property set to true..
I added that (in parent's hbm) and it work as U described.

No JIRA issue needed :-)

regards


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.