Why is NHibernate performing an INSERT instead of an update?

Why is NHibernate performing an INSERT instead of an update?

I have the following mapping:

<!-- WidgetConfiguration --> <class name="MyProject.WidgetConfiguration, MyProject" table="WidgetConfigurations">   <id name="Id" column="Id" type="Int64">     <generator class="native" />   </id>    <property name="Name" column="ConfigurationName" />    <map name="Widgets" table="WidgetConfigurationPositions" cascade="all" lazy="false" fetch="select" inverse="true">     <key column="WidgetConfigurationId" />     <index column="TargetId" type="string" />     <one-to-many class="MyProject.WidgetPlacement" />   </map> </class> <!-- End WidgetConfiguration -->  <class name="MyProject.WidgetPlacement, MyProject" table="WidgetConfigurationPositions">   <id name="Id" column="Id" type="Int64">     <generator class="native" />   </id>    <many-to-one name="Widget" class="MyProject.Widget, MyProject" column="WidgetId" lazy="false" />   <property name="Target" column="TargetId" not-null="true" />    <map name="Options" table="PlacedWidgetOptions" cascade="all" lazy="false" fetch="select">     <key column="WidgetConfigurationPositionId"/>     <index column="OptionName" type="string" />     <element column="OptionValue" type="string" />   </map> </class> 

And, I have the following bit of code:

public override void Update(WidgetConfiguration obj) {  using (var session = GetSession())  {   var tx = session.BeginTransaction();   session.Update(obj);   tx.Commit();    //session.Evict(obj);  } } 

I can Save a WidgetConfiguration just dandy, but trying to UPDATE a WidgetConfiguration, NHibernate actually performs an insert! Here is the session from my NHibernate Profiler.

begin transaction with isolation level: Unspecified  INSERT INTO WidgetConfigurations            (ConfigurationName) VALUES     ('dashboard' /* @p0 */) select SCOPE_IDENTITY()  INSERT INTO WidgetConfigurationPositions            (WidgetId,             TargetId) VALUES     (1 /* @p0 */,             'row1-column1' /* @p1 */) select SCOPE_IDENTITY()  UPDATE WidgetConfigurationPositions SET    WidgetId = 1 /* @p0 */,        TargetId = 'row1-column2' /* @p1 */ WHERE  Id = 356 /* @p2 */  commit transaction 

I have no idea why this would be happening and Google is not being very helpful. Anyone have any ideas?

Should my ViewModel's constructor populate the ViewModel's data?


Jquery problem:validation code not working
Try setting the unsaved-value attrialthough e:. How to make selectedItem in a SelectList be dynamic
<id name="Id" column="Id" type="Int64" unsaved-value="0">     <generator class="native" /> </id> 
What is the type of the ID column? If it's long? then unsaved-value should be null. Master ASP.NET MVCYou shouldn't need to set this although it's the first thing I would try.. How do I convert DateTime format to ShortDateString in Jquery?
Why is VS2010 in ASP.NET MVC2 giving me the wrong JQuery intellisense?Unit testing with EF4 “Code First” and Repository


Passing nested arrays to asp.net mvc using jQuery's $.ajax
Are you remembering to override your Equals() and GetHashCode()? Since you are detaching and then re-attaching these methods will come into play. . I'd recommend picking up NHibernate In Action and reading around persistence lifecycle :).


My guess is this your problem is caused by retrieving the object in one NH session and updating it in another.. Either keep the NH session open throughout the request, or try to attach (Lock() ?) the object to the updating NH session..

70 out of 100 based on 20 user ratings 100 reviews