-->
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.  [ 1 post ] 
Author Message
 Post subject: StaleObjectStateException when SaveOrUpdate and inheritance
PostPosted: Fri Aug 17, 2007 10:14 am 
Newbie

Joined: Wed Nov 22, 2006 7:28 am
Posts: 12
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.