-->
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: I can't get it to delete a child.
PostPosted: Tue Jan 31, 2006 5:07 pm 
Newbie

Joined: Wed Jan 25, 2006 10:53 pm
Posts: 9
I have a simple scenario, I have a Parent-Child relationship. I want to add and remove children. If I do either add or delete it seems to work, but cannot get it to do both in one transaction. It tries to do the insert before the delete which causes a contraint violation. I have tried do the insert and delete both ways round. I am new to NHibernate, just started a new project where we want to use it but are nrevous that little simple problems like this are going to cause this project to fail. I could do this in SQL in 30secs!!!!

Thanks for any help

Tim

This is what I do....

//Load the object
int entryId = 1;
Entry result = session.Load(typeof(Entry), entryId) as Entry;

//Add a new item
newEntryPick = new EntryPick();
newEntryPick.MarketId = newPick.MarketId;
newEntryPick.SelectionId = newPick.SelectionId;
entry.AddPick(newEntryPick);

//Delete one of the origional items
//Not sure how to do this? Set it parent to null? Simple remove it from
// the collection, I thought this shiould work as I have cascade all-delete-//orphan set? Explicitly call Delete for each child? (I have tried all
//of these)
entry.Picks.Remove(picktoRemove);

//Save the object
session.SaveOrUpdate(entry);

//Here I am expecting one delete, followed by one insert?






NHibernate version: 1.0.2.0

Mapping documents:

Parent:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Entry" table="Entry">

<id name="m_Id" column="Id" type="int" access="field">
<generator class="identity" />
</id>

<property name="m_UserId" column="UserId" type="int" access="field" />

<set name="m_Picks" table="EntryPick" access="field" inverse="true" cascade="all-delete-orphan" >
<key column="EntryId" />
<one-to-many class="EntryPick"/>
</set>

</class>
</hibernate-mapping>

Child:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="EntryPick" table="EntryPick">
<id name="m_Id" column="Id" type="int" access="field">
<generator class="identity" />
</id>
<!--property name="m_EntryId" column="EntryId" type="int" access="field" /-->
<property name="m_MarketId" column="MarketId" type="int" access="field" />
<property name="m_SelectionId" column="SelectionId" type="int" access="field" />

<many-to-one
name="m_Entry"
access="field"
column="EntryId"
not-null="true"
class="Entry"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 5:30 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
The order of operations during Flush is shown here:
http://www.hibernate.org/hib_docs/reference/en/html/manipulatingdata.html#manipulatingdata-flushing

If you need to perform operations in different order, you can remove the item from the collection, call Delete, then explicitly call Flush and then add the new item.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 6:17 pm 
Newbie

Joined: Wed Jan 25, 2006 10:53 pm
Posts: 9
Thanks, that worked.

I sure there is a very good reason for doing the inserts before the deletes made by very clever people, but I don't understand why it is doen this way!!!!

Thanks again

Tim


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.