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