Thanks, I tried. As the post said, I changed the mapping sittings like:

<set name="Properties" cascade="all(or save-update)" lazy="true" 
inverse="true">
  <key column="ProductID" on-delete="cascade" />
  <one-to-many class="Property"/>
</set>

And now NH just execute an DELETE sql as I want. But, I cannot correctly 
insert the data now:

var product = new Product { Name = "Product" };
product.Properties.Add(new Property { Name = "P0", SortOrder = 0 });
product.Properties.Add(new Property { Name = "P1", SortOrder = 1 });
session.Save(product);
session.Flush();

But now the data in database is:

table Product
ProductID   Name
----------- --------------------------------------------------
9           Product

table Property
PropertyID  ProductID   Name 
SortOrder
----------- ----------- -------------------------------------------------- 
-----------
12          0           P0                                                 0
13          0           P1                                                 1

The ProductID column in Property should be 9 but 0.

// PS: I cannot find the discription of on-delete attribute in the doc: 
http://nhforge.org/doc/nh/en/index.html.



Blog: http://www.cnblogs.com/JeffreyZhao/
Twitter: http://twitter.com/jeffz_cn

--------------------------------------------------
From: "Stefan Steinegger" <[email protected]>
Sent: Friday, August 14, 2009 5:59 PM
To: "nhusers" <[email protected]>
Subject: [nhusers] Re: How can I JUST delete an entity using NHibernate

>
> Try
>
> Product product = session.Load<Product>(1);
> session.Delete(product);
>
> This should actually only create a proxy for the Product 8assuming you
> are using lazy loading). Then you can remove it.
>
> then you can use on-delete in the mapping to let the database clean up
> referenced records:
> http://vanryswyckjan.blogspot.com/2008/04/nhibernate-20-and-cascading-deletes.html
>
>
> On 14 Aug., 11:12, "Jeffrey Zhao" <[email protected]> wrote:
>> Hello,
>>
>> I'm a new for NH and I met the problem of "how can I delete an entity". 
>> For example, I've got two entity with an one-to-many association.
>> Here's the sql to build the table (omit the association):
>>
>> CREATE TABLE [dbo].[Property](
>>     [PropertyID] [int] IDENTITY(1,1) NOT NULL,
>>     [ProductID] [int] NOT NULL,
>>     [Name] [nvarchar](50) NOT NULL,
>>     [SortOrder] [int] NOT NULL,
>>  CONSTRAINT [PK_Property] PRIMARY KEY CLUSTERED
>> (
>>     [PropertyID] ASC
>> ))
>> GO
>>
>> CREATE TABLE [dbo].[Product](
>>     [ProductID] [int] IDENTITY(1,1) NOT NULL,
>>     [Name] [nvarchar](50) NOT NULL,
>>  CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
>> (
>>     [ProductID] ASC
>> ))
>> GO
>>
>> Here comes the entities:
>>
>> public class Product
>> {
>>     public virtual int ProductID { get; set; }
>>     public virtual string Name { get; set; }
>>     public virtual ISet<Property> Properties { get; set; }}
>>
>> public class Property
>> {
>>     public virtual int PropertyID { get; set; }
>>     public virtual int ProductID { get; set; }
>>     public virtual string Name { get; set; }
>>     public virtual int SortOrder { get; set; }
>>
>> }
>>
>> and my hbm.xml file:
>>
>> <?xml version="1.0" encoding="utf-8" ?>
>> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHTest" 
>> namespace="NHTest">
>>   <class name="Product" table="Product">
>>     <id name="ProductID" column="ProductID">
>>       <generator class="identity"/>
>>     </id>
>>     <property name="Name"/>
>>     <set name="Properties" cascade="all">
>>       <key column="ProductID"/>
>>       <one-to-many class="Property"/>
>>     </set>
>>   </class>
>>   <class name="Property">
>>     <id name="PropertyID">
>>       <generator class="identity"/>
>>     </id>
>>     <property name="Name"/>
>>     <property name="SortOrder"/>
>>     <property name="ProductID" />
>>   </class>
>> </hibernate-mapping>
>>
>> well, that's quite simple, and I can insert one Product with two 
>> Properties without problem, but how can I delete the Product with ID 
>> equals 1? I tried:
>>
>> session.Delete(new Product { ProductID = 1 });
>> session.Flush();
>>
>> But NH always want to update the properties' ProductID to NULL, but my 
>> schema won't accept the change (for NOT NULL). I tried every cascade 
>> settings in the <set /> element but always faild by updating.
>> I just want to execute the SQL like "DELETE FROM Product WHERE ProductID 
>> = 1" and everything else could be done in database. (e.g., cascade 
>> deletion).
>> What should I do?
>>
>> Blog:http://www.cnblogs.com/JeffreyZhao/
>> Twitter:http://twitter.com/jeffz_cn
> >
> 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to