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.  [ 5 posts ] 
Author Message
 Post subject: Problem with Flush()
PostPosted: Thu Sep 28, 2006 8:49 am 
Regular
Regular

Joined: Thu May 11, 2006 12:30 pm
Posts: 72
i'm trying to change the primary key of an instantited object but i'm getting this exception

Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

if i change other properties i've no problem

Hibernate version:
1.2.0 Beta 1
Mapping documents:
<?xml version='1.0' encoding='utf-8'?>
<hibernate-mapping xmlns='urn:nhibernate-mapping-2.0'>
<class name='RuredilManager.Impegni, RuredilManager' table='IMPEGNI'>
<composite-id>
<key-property name='UserID' column='IMPUSER' />
<key-property name='AppointmentDatetime' column='IMPDATE' />
<key-property name='Hours' column='IMPHOUR' />
<key-property name='Minutes' column='IMPMINUTE' />
</composite-id>
<version name='Version' column='IMPVERSION' unsaved-value='99' />
<property name='Text' column='IMPTEXT' />
<property name='Owner' column='IMPOWNER' />
<property name='Host' column='IMPHOST' />
<property name='InsertDatetime' column='IMPDATETIME' />
<property name='UpdateOwner' column='IMPUPDOWNER' />
<property name='UpdateHost' column='IMPUPDHOST' />
<property name='UpdateDatetime' column='IMPUPDDATETIME' />
<property name='Attach' column='IMPATTACH' />
<property name='AttachType' column='IMPATTACHTYPE' />
<property name='Description' column='IMPDESC' />
<many-to-one class='RuredilManager.TipiImpegno, RuredilManager' name='AppointmentType' column='IMPTYPE' />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
this.m_job.UserID = this.m_user.ID;
this.m_job.AppointmentDatetime = this.m_appointment.StartTime;
this.m_job.AppointmentType = this.m_session.Get(typeof(TipiImpegno), int.Parse(cmbTypes.SelectedValue.ToString())) as TipiImpegno;
this.m_job.Description = txtDescription.Text.Trim();
this.m_job.Host = Environment.MachineName;
this.m_job.UpdateHost = Environment.MachineName;
this.m_job.InsertDatetime = DateTime.Now;
this.m_job.UpdateDatetime = DateTime.Now;
this.m_job.Hours = int.Parse(mtxtDuration.Text.Split(new char[] { '.' })[0]);
this.m_job.Minutes = int.Parse(mtxtDuration.Text.Split(new char[] { '.' })[1]);
this.m_job.Owner = Environment.UserName;
this.m_job.UpdateOwner = Environment.UserName;
this.m_job.Text = Encoding.UTF8.GetBytes(rteText.RichTextBox.Rtf.Trim());
this.m_job.Version = 99;
this.m_session.Update(this.m_job);
this.m_session.Flush();
Full stack trace of any exception that occurs:
in NHibernate.Persister.Entity.AbstractEntityPersister.Check(Int32 rows, Object id, Int32 tableNumber, IExpectation expectation, IDbCommand statement) in C:\Documents and Settings\luke\Desktop\NHibernate Beta 1\NHibernate\Persister\Entity\AbstractEntityPersister.cs:riga 619
in NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session) in C:\Documents and Settings\luke\Desktop\NHibernate Beta 1\NHibernate\Persister\Entity\AbstractEntityPersister.cs:riga 1833
in NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, ISessionImplementor session) in C:\Documents and Settings\luke\Desktop\NHibernate Beta 1\NHibernate\Persister\Entity\AbstractEntityPersister.cs:riga 1734
in NHibernate.Impl.ScheduledUpdate.Execute() in C:\Documents and Settings\luke\Desktop\NHibernate Beta 1\NHibernate\Impl\ScheduledUpdate.cs:riga 63
in NHibernate.Impl.SessionImpl.Execute(IExecutable executable) in C:\Documents and Settings\luke\Desktop\NHibernate Beta 1\NHibernate\Impl\SessionImpl.cs:riga 3283
in NHibernate.Impl.SessionImpl.ExecuteAll(IList list) in C:\Documents and Settings\luke\Desktop\NHibernate Beta 1\NHibernate\Impl\SessionImpl.cs:riga 3262
in NHibernate.Impl.SessionImpl.Execute() in C:\Documents and Settings\luke\Desktop\NHibernate Beta 1\NHibernate\Impl\SessionImpl.cs:riga 3214
in NHibernate.Impl.SessionImpl.Flush() in C:\Documents and Settings\luke\Desktop\NHibernate Beta 1\NHibernate\Impl\SessionImpl.cs:riga 3050
in RuredilManager.frmImpegno.kbConfirm_Click(Object sender, EventArgs e) in C:\Documents and Settings\luke\Desktop\RuredilManager\RuredilManager\frmImpegno.cs:riga 83
in System.Windows.Forms.Control.OnClick(EventArgs e)
in ComponentFactory.Krypton.Toolkit.KryptonButton.OnClick(EventArgs e)
in ComponentFactory.Krypton.Toolkit.KryptonButton.OnButtonClick(Object sender, MouseEventArgs e)
in ComponentFactory.Krypton.Toolkit.ButtonController.OnClick(MouseEventArgs e)
in ComponentFactory.Krypton.Toolkit.ButtonController.MouseUp(Control c, Point pt, MouseButtons button)
in ComponentFactory.Krypton.Toolkit.ViewBase.MouseUp(Point pt, MouseButtons button)
in ComponentFactory.Krypton.Toolkit.ViewBase.MouseUp(Point pt, MouseButtons button)
in ComponentFactory.Krypton.Toolkit.ViewBase.MouseUp(Point pt, MouseButtons button)
in ComponentFactory.Krypton.Toolkit.ViewManager.MouseUp(MouseEventArgs e)
in ComponentFactory.Krypton.Toolkit.VisualControl.OnMouseUp(MouseEventArgs e)
in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
in System.Windows.Forms.Form.ShowDialog()
in RuredilManager.frmMain._ManageAppointment(ScheduleAppointment Appointment) in C:\Documents and Settings\luke\Desktop\RuredilManager\RuredilManager\frmMain.cs:riga 137
in RuredilManager.frmMain.schMain_AppointmentDoubleClick(Object sender, AppointmentEventArgs e) in C:\Documents and Settings\luke\Desktop\RuredilManager\RuredilManager\frmMain.cs:riga 165
in Janus.Windows.Schedule.Schedule.OnAppointmentDoubleClick(AppointmentEventArgs e)
in Janus.Windows.Schedule.Schedule.y(ScheduleAppointment )
in Janus.Windows.Schedule.JNSAR.a(EventArgs )
in Janus.Windows.Schedule.Schedule.OnDoubleClick(EventArgs e)
in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.Run(Form mainForm)
in RuredilManager.Program.Main() in C:\Documents and Settings\luke\Desktop\RuredilManager\RuredilManager\Program.cs:riga 17
in System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart()
Name and version of the database you are using: DB2/400


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 9:26 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Changing the primary key of an entity is not allowed.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 9:31 am 
Regular
Regular

Joined: Thu May 11, 2006 12:30 pm
Posts: 72
ok, as I thought .

so the right way is clone my entity (that implement ICloneable) make my changes to the cloned, save the new one and delete the older : right ?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 9:42 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
The completely right way is to avoid such changes entirely... Maybe do it outside of NHibernate using direct SQL. Of course you can delete the old entity and insert a new one with the given primary key, but it means that any collections the old entity had will either be gone, or will have to be re-inserted, this may be inefficient.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 11:41 pm 
Regular
Regular

Joined: Tue Feb 21, 2006 9:50 am
Posts: 107
I always use technical keys like Guid's or sequeneces as primary key so i don't have the need to change them. The nature of functional primary keys is that they change over the time. If you have such keys as foreign keys in a large database you may become trouble because of the heavy updates needed for the changes.
So my functional keys are always modeled as normal or unique (if required) indexes in the database and the primary keys and relationships are always artificial values with no meaning. On Sql-Server i prefer to use Guid's as primary key values on Oracle i use sequences.

Regards
Klaus


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.