-->
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.  [ 2 posts ] 
Author Message
 Post subject: SaveOrUpdate Problem
PostPosted: Sun Dec 09, 2007 11:30 am 
Newbie

Joined: Sun Nov 18, 2007 3:22 am
Posts: 8
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 9:49 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
Yes, you need to load the object in advance. The point of using SaveOrUpdate is really to associate objects with the session. Lets say that you load an object from the database, disconnect the session and then later want to update that object. In this case you would call SaveOrUpdate and the version property would already be assigned, therefore it should work unless someone update the object.

The problem with calling SaveOrUpdate without setting the version (for the second call) is that it will try to insert the record (since unsaved-value="0" for the version) which results in a primary key conflict.

Note that if you set the version to the wrong version it would also throw an exception since no row with that id and version would be found. If you are trying to use SaveOrUpdate because you are receiving the entire object via some external source, I would instead recommend loading the object by primary key, making the modifications which you were provided and then flushing the session.


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

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.