Hi,
Basically, Derek's suggestion of using cascade="save-update" helped, but I think the actual problem was in the table structure.
There should not be any foreign key from the child table to the parent table.
Please see the table definitions, mapping files and the C# classes below (I have a one-to-one relationship between a Product and a Stock)-
Tables -
Code:
CREATE TABLE [dbo].[Product](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Description] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[CompanyId] [int] NOT NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Stock](
[Id] [int] NOT NULL,
[Quantity] [int] NOT NULL,
CONSTRAINT [PK_Stock] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Mapping files-
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Care.Inventory.Product, Care.Inventory" table="Product">
<id name="Id" type="Int32" unsaved-value="null">
<column name="Id" length="4" sql-type="int" not-null="true" unique="true" index="PK_Product"/>
<generator class="native" />
</id>
<property name="Name" column="Name" type="String" length="50" not-null="true" />
<property name="Description" column="Description" type="String" length="50" not-null="false" />
<one-to-one name="Stock" class="Care.Inventory.Stock, Care.Inventory" cascade="all-delete-orphan" />
<many-to-one name="Company" column="CompanyId" class="Care.Inventory.Company, Care.Inventory" not-null="true" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Care.Inventory.Stock, Care.Inventory" table="Stock">
<id name="Id" column="Id" type="Int32">
<generator class="foreign">
<param name="property">Product</param>
</generator>
</id>
<property name="Quantity" column="Quantity" type="Int32" length="4" not-null="true" />
<one-to-one name="Product" class="Care.Inventory.Product, Care.Inventory" constrained="true" />
</class>
</hibernate-mapping>
The classes-
Code:
using System;
using System.Collections.Generic;
namespace Care.Inventory {
public class Product {
private int id;
private string name;
private string description;
private Stock stock;
private Company company;
public Product() {
name = String.Empty;
description = String.Empty;
company = new Company();
stock = new Stock();
}
public virtual int Id {
get { return id; }
set { id = value; }
}
public virtual string Name {
get { return name; }
set { name = value; }
}
public virtual string Description {
get { return description; }
set { description = value; }
}
public virtual Stock Stock {
get { return stock; }
set {
stock = value;
stock.Product = this;
}
}
public virtual Company Company {
get { return company; }
set { company = value; }
}
}
}
using System;
using System.Collections.Generic;
namespace Care.Inventory {
public class Stock {
private int id;
private int quantity;
private Product product;
public virtual int Id {
get { return id; }
set { id = value; }
}
public virtual int Quantity {
get { return quantity; }
set { quantity = value; }
}
public virtual Product Product {
get { return product; }
set { product = value; }
}
}
}
Note that I've used
cascade="all-delete-orphan" so that when you delete a Product the corresponding Stock record also gets deleted.
Hope it helps.
Regards,
Manoj.