HI,
I am trying to use an SaveOrUpdate for an object that has an assigned PK and a none key Identity.
I saw in other posts that for assigned ID i must use versioning. i guess i am using it wrong but i cant figure out what is wrong.
this is my mapping file(i tried to use undefined instead of 0 as well):
<class name="Root" table="XXXParent">
<id name="PID">
<generator class="assigned" />
</id>
<version name="version" column="version" type="integer" unsaved-value="0"/>
<property name="PIdentity" column="mPIdentity" update="false" insert="false"/>
<property name="Date" column="sDate" type="DateTime"/>
</class>
The Code i am trying to run:
using (ISession session = NHibernateHelper.GetCurrentSession(context))
{
using(ITransaction trans = session.BeginTransaction())
{
session.Clear();
Root x = new Root();
x.PID = 800; x.Date = DateTime.Now;
session.SaveOrUpdate(x);
trans.Commit();
}
}
NHibernateHelper.CloseSession();
using (ISession session1 = NHibernateHelper.GetCurrentSession(context))
{
using(ITransaction trans = session1.BeginTransaction())
{
session1.Clear();
Root x1 = new Root();
x1.PID = 800; x1.Date = DateTime.Now;
//x.version = 1;
session1.SaveOrUpdate(x1);
trans.Commit();
}
}
NHibernateHelper.CloseSession();
The first Save succeded but the updated throw an exception.
When i specified the version =1 then it worked but off course i dont know in advance if the object exists ot or not in the DB.
The DB Schema:
CREATE TABLE [dbo].[XXXParent](
[mPIdentity] [int] IDENTITY(1,1) NOT NULL,
[PID] [int] NOT NULL,
[sDate] [datetime] NOT NULL,
[version] [int] NULL,
CONSTRAINT [PK_XXXParent] PRIMARY KEY CLUSTERED
([PID] ASC)) ON [PRIMARY]
Do i have to load the object?????
Thx,
Tomer
|