NHibernate 1.2.00.GA.
I've started developing a system and will be using the Table Per Subclass strategy to map inheritance in the database.
The database schema consists of two tables.
Party : base class of any acting entity to the system.
Person : class that inherits from Party which represents a person.
SQL to create Party:
CREATE TABLE [dbo].[Party] (
[partyId] [int] IDENTITY (1, 1) NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Party] WITH NOCHECK ADD
CONSTRAINT [PK_Party] PRIMARY KEY CLUSTERED
(
[partyId]
) ON [PRIMARY]
GO
SQL to create Person:
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].[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
So we've got:
Party.partyId set as identity column and primary key
Person.partyId set as primary key and a foreign key to Party.PartyId.
PS: the NHibernate documentation is not very clear as to what constraints should be done on the database side when setting up Table Per Subclass.
Now the NHibernate mapping:
PARTY:
<class name="Party" table="Party">
<id name="id"
column="partyId"
type="Int32"
access="field"
unsaved-value="0">
<generator class="native" />
</id>
</class>
PERSON:
<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>
And that's it...
Now I can create a Party entity and have it persisted onto the database but I can't do this for Person.
If I try:
Person p = new Person(); p.FirstName = "foo"; p.LastName = "bar"; IoC.Resolve<IRepository<Person>>().SaveOrUpdate(p);
I will get the following exception:
NHibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for XXX.Parties.Person instance with identifier: 9
Stacktrace:
at NHibernate.Persister.Entity.AbstractEntityPersister.Check(Int32 rows, Object id, Int32 tableNumber, IExpectation expectation, IDbCommand statement)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledIdentityInsertion.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.DoSave(Object theObj, EntityKey key, IEntityPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IEntityPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj)
The SQL generated by NHibernate seemed ok to me:
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 = '9'
|