NHibernate 1.2.0.GA
I have got a pretty simple inheritance tree and I am struggling against an error which occurs when trying to SaveOrUpdate an entity.
When I do this:
Person p = new Person();
p.FirstName = "foo";
p.LastName = "bar";
IoC.Resolve<IRepository<Person>>().SaveOrUpdate(p);
I will get two correct SQL queries:
INSERT INTO dbo.Party DEFAULT VALUES; select SCOPE_IDENTITY()
INSERT INTO dbo.Person (firstName, lastName, partyId) VALUES (@p0, @p1, @p2); @p0 = 'foo', @p1 = 'bar', @p2 = '23'
But I will then get an exception:
NHibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for Bbc.Ww.ContentContribution.DomainModel.Person instance with identifier: 23
I have tried as well running this code within an ADO.Net transaction but it doesn't help.
Any idea??
DOMAIN MODEL:
public class Party {
private int id = 0;
}
and
public class Person : Party {
private string firstName;
private string surName;
}
MAPPING DOCUMENTS:
<class name="Party" table="Party">
<id name="id" column="partyId" type="Int32" access="field" unsaved-value="0">
<generator class="native" />
</id>
</class>
and
<joined-subclass name="Person" table="Person" extends="Party">
<key column="partyId"/>
<property name="FirstName" column="firstName" type="String" length="255"/>
<property name="LastName" column="lastName" type="String" length="255"/>
</joined-subclass>
DATABASE SCHEMA:
CREATE TABLE [dbo].[Party] (
[partyId] [int] IDENTITY (1, 1) NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Person] (
[partyId] [int] NOT NULL ,
[firstName] [varchar] (150) COLLATE Latin1_General_CI_AI NULL ,
[lastName] [varchar] (150) COLLATE Latin1_General_CI_AI NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Party] WITH NOCHECK ADD
CONSTRAINT [PK_Party] PRIMARY KEY CLUSTERED
(
[partyId]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Person] WITH NOCHECK ADD
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
(
[partyId]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Person] ADD
CONSTRAINT [FK_Person_Party] FOREIGN KEY
(
[partyId]
) REFERENCES [dbo].[Party] (
[partyId]
)
GO
|